Reputation: 149
I have a problem which should be to hard but I can't get it the way i want. My df have about 200 rows and almost 4200 columns. The only thing that exist are 0, 1 and 2 in the df. Something like:
Col1 Col2 Col3 ...
Row1 2 1 0
Row2 1 1 1
Row3 2 2 2
.
.
I now want to save how many times number 2 apper in each coloumn so i wrote
freq<-sapply(df,table)
but here is where I get stuck. I want to save how many 2:s there was in every coloumn and save it in a ny df and keep track of which coloumn had that many 2:s.
Want it to look something like this (if it was done on the example above):
Col1 Col2 Col3
2 1 1
Sorry that I explaning a bit bad right now. Please tell me if I should add something to make it easier to understad my problem.
Upvotes: 1
Views: 53
Reputation: 7200
Another way in base R. Just use the table
function as you did previously and then subset to obtain only the values you need (in this case 2).
dft <- sapply(df1, table)
lapply(dft, function(x) x[[2]] )
$Col1
[1] 2
$Col2
[1] 1
$Col3
[1] 1
Upvotes: 1
Reputation: 887941
You can try colSums
colSums(df==2)
#Col1 Col2 Col3
# 2 1 1
We create a logical matrix (df==2
) and then get the sum
of each column by colSums
For big datasets, it may be better to loop as the big matrix from df==2
may cause memory issues
vapply(df, function(x) sum(x==2), numeric(1))
df <- structure(list(Col1 = c(2L, 1L, 2L), Col2 = c(1L, 1L, 2L),
Col3 = 0:2), .Names = c("Col1",
"Col2", "Col3"), class = "data.frame", row.names = c("Row1",
"Row2", "Row3"))
Upvotes: 2