0

I have a .txt file in which there are 13 columns. the first one is Characters(names) and the next 12 are numbers. also there are 1000 rows. I want to filter out the rows in which even one column has the value less than 10. in other word I just need the rows with values equal or more than 10 in all columns. could you please let me know how I can do that in R?

thanks.

user3925736
  • 99
  • 1
  • 1
  • 13
  • 3
    Can you provide a reproducible example? http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – ArunK Jun 06 '16 at 09:34
  • 2
    Looks like your first or early post. Providing sample input and sample output is the best way to get help. You can try something like this for example with a made up data frame: `df <- data.frame(x = 'a', y = sample(c(9, 10), 10, replace = TRUE), z = sample(c(9, 10), 10, replace = TRUE)); df[!rowSums(df[, -1] < 10), ]` – Gopala Jun 06 '16 at 09:40
  • @Gopala: this is the results I got "There were 12 warnings (use warnings() to see them)" – user3925736 Jun 06 '16 at 09:56
  • A commenter will only get a notification if you put an @ before their name, for example @Gopala. Regarding your question, you could use something like `apply(x>10,1,all)`, but this will also check your first column of characters – Steve Jun 06 '16 at 09:56
  • Similar to @Gopala solution, you can try `df[rowMeans(df[,-1] >= 10),]`, since any row with at least 1 value less than 10 will drop the mean below 10. – Sotos Jun 06 '16 at 10:11

1 Answers1

0

You can use the which() function in R to satisfy your condition. Create some test data:

> test X1 X2 X3 X4 1 9.725585 10.067146 9.473320 9.959529 2 10.104124 11.278900 9.299356 10.317570 3 8.770733 11.092994 9.803285 12.078180 4 10.163150 9.233452 9.425293 9.968435 5 9.815270 9.932501 9.798252 9.194674 6 10.635158 9.175388 10.938356 10.611528 7 10.959444 7.766411 8.955005 10.712767 8 9.907442 10.123078 9.897276 10.467526 9 9.337628 10.811072 11.062031 10.426313 10 10.056789 11.029007 10.875958 11.160633

using which(test < 10, arr.ind = TRUE) gives:

> head(which(test < 10, arr.ind = TRUE))
      row col
[1,]   1   1
[2,]   3   1
[3,]   5   1
[4,]   8   1
[5,]   9   1
[6,]   4   2

Then:

> sort(unique(which(test < 10, arr.ind = TRUE)[, 1]))
[1] 1 2 3 4 5 6 7 8 9
catalandres
  • 1,149
  • 8
  • 20
Andrew Haynes
  • 2,612
  • 2
  • 20
  • 35