R How to count occurrences of values across multiple columns of a data frame and save the columnwise counts from a particular value as a new row?

user2903950 picture user2903950 · Nov 30, 2013 · Viewed 20.4k times · Source

I have a large data-frame (approx 1,000 rows and 30,000 columns) that looks like this:

   chr pos  sample1 sample2 sample3 sample 4
    1 5050    1       NA      0       0.5
    1 6300    1       0       0.5     1
    1 7825    1       0       0.5     1
    1 8200    0.5     0.5     0       1

where at a given "chr"&"pos" the value for a given sample can take the form of 0, 0.5, 1, or NA. I have a large number of queries to perform that will require subsetting and ordering the data frame based on summaries of the values for each sample.

I would like to get a count of the number of occurrences of a given value (e.g. 0.5) for each column, and save that as a new row in my data frame. My ultimate goal is to be able to use the values of the new row to subset and/or order the columns of my data frame. I've seen similar questions about counting occurrences, but I can't seem to find/recognize a solution to doing this across all columns simultaneously and saving the column-wise counts for a particular value as a new row.

Answer

Donbeo picture Donbeo · Nov 30, 2013

you can apply a function to all the column of you data.frame. Suppose you want to count the number of 'A' in each column of the data.frame d

#a sample data.frame
    L3 <- LETTERS[1:3]
     (d <- data.frame(cbind(x = 1, y = 1:10), fac = sample(L3, 10, replace = TRUE)))



# the function you are looking for
    apply(X=d,2,FUN=function(x) length(which(x=='A')))