This is a test version of Biostars. For the public version, visit https://www.biostars.org.
filter data based on values

I have a very simple question, but sometime one get stuck with simple problems. I have a data file with 500 rows and 2000 columns. I want to filter and subset this data based on if cells have value more than or equal to 2. I know filter of columns is easy. but how can I subset this data retaining the original header information.

Thanks

general

Need some more information. If you find one cell in a row is less than 2, do you want to remove the entire row? Same question for the columns as well.

2 answers

If you need to preserve the header:

$ head -1 input.mtx > header.txt
$ awk -vOFS="\t" '{ flag = 1; for (i = 1; i <= NF; i++) { if ($i < 2) { flag = 0; break; } } if (flag == 1) { print $0 } }' <(tail -n+2 input.mtx) > output.woHeader.txt
$ cat header.txt output.woHeader.txt > output.mtx

Using R this will only keep rows where all values are greater than or equal to 2.

index <- data >= 2
index <- apply(index, 1, all)

data <- data[index,]

Log in to answer this question.