2

I'm currently learning R with help of video on coursera. When trying to exclude all hospital of state which have less than 20 hospital form table, I couldn't able to find correct solution with lack of programming knowledge of R (as I had program lots with C, Logic I tried to implemented in R is also like C)

Code I had used is like

>test <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
>test[, 11] <- as.numeric(outcome[, 11])
>test2 <- table(outcome$State)

Here from table test2, I can get the value of particular row as test2[[2]] but couldn't able to find out how to use conditional logic to get state with less then 20 hospital (If i get the state name then I can use subset() to address actual problem). Also I had look on dimnames() function but could find out any idea to solve my problem. So my question is, in R how could I check the threshold value with table value.

Value store in test2 is

 AK  AL  AR  AZ  CA  CO  CT  DC  DE  FL  GA  GU  HI  IA  ID  IL  IN  KS  KY  LA  MA  MD  ME 
 17  98  77  77 341  72  32   8   6 180 132   1  19 109  30 179 124 118  96 114  68  45  37 
 MI  MN  MO  MS  MT  NC  ND  NE  NH  NJ  NM  NV  NY  OH  OK  OR  PA  PR  RI  SC  SD  TN  TX 
134 133 108  83  54 112  36  90  26  65  40  28 185 170 126  59 175  51  12  63  48 116 370 
 UT  VA  VI  VT  WA  WI  WV  WY  ##State Name
 42  87   2  15  88 125  54  29  ##Count of Hospital
Lionel
  • 604
  • 9
  • 26
  • 2
    the output of `table` is a vector. so, it is sufficient `test2[2]` (to use one `[]`) to access the element. Two `[[]]` are used for list. Now to answer your question: `test2[test2 >= 20]` should give you all values >= 20. And `names(test2[test2 >= 20])` will give you the states. – Arun Feb 01 '13 at 16:16

2 Answers2

3

as Arun also specified on his comment... you can do it as names(test2[test2 >= 20]) in order to get state with higher than 20 Hospital... Here is nice explanation why you have to avoid subset.

Community
  • 1
  • 1
Krish
  • 70
  • 8
2

Or yo can transform your table to a data.frame and use subset

dat <- as.data.frame(test2)
subset(dat, Freq < 20)
   nn Freq
1  AK   17
8  DC    8
9  DE    6
12 GU    1
13 HI   19
42 RI   12
49 VI    2
50 VT   15
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • 3
    Just thought I'd point to this nice post on [**some disadvantages of using subset**](http://stackoverflow.com/questions/9860090/in-r-why-is-better-than-subset) in other than interactive sessions. – Arun Feb 01 '13 at 16:32