0

I searched many questions that were suggested by the stackoverflow before posting this question but I couldn't find what I was looking for, I decided to ask here, I have data file: https://github.com/learnseq/learning/blob/main/GSE133399_Fig2_FPKM.csv

The file has 9 columns, first column has names, the other 8 columns have values, I want to render into an object all columns that do not have zero and save the in csv format.

user432797
  • 593
  • 4
  • 13
  • Possible duplicates - https://stackoverflow.com/questions/25203813/remove-rows-from-dataframe-that-contains-only-0-or-just-a-single-0 or https://stackoverflow.com/questions/25183844/r-i-want-to-go-through-rows-of-a-big-matrix-and-remove-all-zeros/25184000 – thelatemail Oct 19 '20 at 21:38

1 Answers1

1

I had a look on your data set: it contains some rows having all values zero, except the identifier. I assume you want to omit the lines being full of zero's. This code does the job:

data1 = read.csv("GSE133399_Fig2_FPKM.csv")
## Apply <all> on each row.  
allZero = apply(data1[, -1] == 0, 1, all)
data2 = data1[!allZero, ]

Now, data2 is the same as data1, but without the rows having only zeros.

Dharman
  • 30,962
  • 25
  • 85
  • 135
lambruscoAcido
  • 548
  • 4
  • 17
  • Thanks @lambruscoAcido, what if I want to filter only values ≥1.0? how can I do it? – user432797 Oct 20 '20 at 03:02
  • 1
    @user432797 if you need only rows having **all** values >= 1.0 then use: `allOK = apply(data1[, -1] >= 1.0, 1, all)` and `data2 = data1[allOK, ]`. – lambruscoAcido Oct 21 '20 at 11:39