I have a dataframe df
. It looks like:
xSample a b c
x 2 0 2
x1 3 0 0
x2 4 0 2
I have this piece of code:
new_df <- as.data.frame(sapply(df[,-1], function(x) sum(as.numeric(x) > 0)))
I want to go through each column of df
and count the number of samples and put that into new_df
, but only if there are > 0 counts per sample A, B, or C... The new_df
should look like this:
NonZeroCounts
a 3
c 2
The b
row is not kept because it has 0 counts in every row.
After running my function mentioned above on my df
, the output is:
xSample NonZeroCounts
a 3
b 0
c 2