Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove rows with 0 values using R

Hi am using a matrix of gene expression, frag counts to calculate differentially expressed genes. I would like to know how to remove the rows which have values as 0. Then my data set will be compact and less spurious results will be given for the downstream analysis I do using this matrix.

Input

gene    ZPT.1   ZPT.0   ZPT.2   ZPT.3   PDGT.1  PDGT.0
XLOC_000001 3516    626 1277    770 4309    9030
XLOC_000002 342 82  185 72  835 1095
XLOC_000003 2000    361 867 438 454 687
XLOC_000004 143 30  67  37  90  236
XLOC_000005 0   0   0   0   0   0
XLOC_000006 0   0   0   0   0   0
XLOC_000007 0   0   0   0   1   3
XLOC_000008 0   0   0   0   0   0
XLOC_000009 0   0   0   0   0   0
XLOC_000010 7   1   5   3   0   1
XLOC_000011 63  10  19  15  92  228

Desired output

gene    ZPT.1   ZPT.0   ZPT.2   ZPT.3   PDGT.1  PDGT.0
XLOC_000001 3516    626 1277    770 4309    9030
XLOC_000002 342 82  185 72  835 1095
XLOC_000003 2000    361 867 438 454 687
XLOC_000004 143 30  67  37  90  236
XLOC_000007 0   0   0   0   1   3
XLOC_000010 7   1   5   3   0   1
XLOC_000011 63  10  19  15  92  228

As of now I only want to remove those rows where all the frag count columns are 0 if in any row some values are 0 and others are non zero I would like to keep that row intact as you can see my example above.

Please let me know how to do this.

like image 257
ivivek_ngs Avatar asked Aug 05 '13 10:08

ivivek_ngs


People also ask

How do you drop a row with zero values in R?

Example: Removing Rows with Zeros Using apply() & all() Functions. This example shows how to get rid of all rows with at least one zero value using the apply and all functions in R. The previous R code returns our new data frame to the RStudio console. As you can see, all rows containing zeros were removed.

How do I remove rows from two conditions in R?

To remove rows of data from a dataframe based on multiple conditional statements. We use square brackets [ ] with the dataframe and put multiple conditional statements along with AND or OR operator inside it. This slices the dataframe and removes all the rows that do not satisfy the given conditions.


1 Answers

df[apply(df[,-1], 1, function(x) !all(x==0)),]
like image 147
bartektartanus Avatar answered Oct 11 '22 16:10

bartektartanus