I have a large dataframe which looks like the toy example below.
Sample1 Sample2 Sample3 Sample4
Gene1 0 0 0 1
Gene2 1 0 0 1
Gene3 1 1 1 0
I want to remove all genes which were equal to 0 in at least two samples. Only Gene3 should remain.
The answer in this question was close but not specific enough for my question. How to remove rows with 0 values using R
df[apply(df[,-1], 1, function(x) !all(x==0)),]
Can it be adjusted to remove rows is x==0 two or more times?