0

I have a table looking as the following:

ID  period 1 period 2 period 3 period 4
A     4       2        25       42
B     3       56        2       45
C     16      1        34       67
D     56      2         8       48

I want to check in R how many times(cols) in each row I get values lower than 10. For example in row A I have two values lower than 10. Any ideas???

I used the quantile values and got the following:

quantile(v[,2:5],na.rm=TRUE) 0% 25% 50% 75% 100% 1.00 2.75 20.50 45.75 67.00

But this is not exactly what I need; I want to know the percentage (or count) of values below 10. I tried using the following and also didn´t work:

limit [1] 10 v$tot<-count(v,c("ID","period1","period2"),wt_var=limit)`

The first few rows of the actual dataset areas follows:

    id    1   2   3   4    5   6   7   8   9  10  11  12  13  14  15  16  17
1 xxxlll  61  36 277 462  211 182  45  41 128 174 179  87  18 NaN NaN NaN NaN
2 ccvvbb 281 340 592 455  496 348 422 491 408 548 596 611 570 580 530 602 614
3 ddffgr 587 964 895 866 1120 725 547  90 NaN NaN NaN NaN NaN NaN NaN NaN NaN
4 rrteww 257 331 320 411  442 316 334 403 355 444 522 661 508 499 520 413 494
5 oiertw 261 NaN NaN NaN  NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
tshepang
  • 12,111
  • 21
  • 91
  • 136
inestr
  • 11
  • 3
  • 4
    This is a fairly basic R question. Can you share what you've tried, where you have looked and why/how it hasn't worked? Right now it sounds like you want us to write your code for you. Also please try to make your question [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Justin Jun 26 '13 at 15:45
  • It is....I just realized it after struggling for quite some time.Sorry! – inestr Jun 26 '13 at 15:53
  • Go ahead and post your answer here and accept it for posterity. – Justin Jun 26 '13 at 15:54
  • Here's a nice intro on subsetting [(LINK)](http://www.ats.ucla.edu/stat/r/faq/subset_R.htm) – Tyler Rinker Jun 26 '13 at 15:55
  • I am new at this site and can´t understand the right way to write code neither find the indications to be clear or who to ask. Can someone give me a hint? – inestr Jun 26 '13 at 19:08

1 Answers1

1

I guess I'll add an answer in case the OP doesn't, but in this case I'd use rowSums and logical comparison...

# '-1' drops the ID column
x <- rowSums( df[ ,-1 ] < 10 )
names(x) <- df$ID
x
#A B C D 
#2 2 1 2 
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
  • thanks for the suggestion. It worked for the exampledataset but not for my actual dataset. I am guessing because I have various NA values x results in a list of NA for each ID. – inestr Jun 26 '13 at 19:03
  • just add `na.rm = TRUE` ... `x <- rowSums( df[ ,-1 ] < 10 , na.rm = TRUE )`. Please read the documentation. – Simon O'Hanlon Jun 26 '13 at 20:00