[R] Selecting rows regarding the frequency of a factor variab le.

Liaw, Andy andy_liaw at merck.com
Wed Jun 29 17:09:51 CEST 2005


See if this does what you want:

> dat <- data.frame(f=factor(sample(letters[1:10], 100, replace=TRUE)),
x=runif(100))
> str(dat)
`data.frame':   100 obs. of  2 variables:
 $ f: Factor w/ 10 levels "a","b","c","d",..: 2 5 10 9 10 3 9 8 3 1 ...
 $ x: num  0.9162 0.0481 0.3048 0.0938 0.8599 ...
> g <- names(which(table(dat$f) > 11))
> g
[1] "c" "j"
> dat[dat$f %in% g,]
   f          x
3  j 0.30477413
5  j 0.85992597
6  c 0.86881528
9  c 0.87317095
16 c 0.84252048
18 j 0.24039606
19 j 0.58927414
21 j 0.10077745
32 j 0.72275870
35 c 0.26001549
37 j 0.09608521
40 c 0.15481625
44 c 0.70203309
47 c 0.95907223
50 j 0.35258966
54 c 0.93422614
58 c 0.36546841
61 c 0.55123183
64 j 0.82995122
65 c 0.89104229
66 j 0.81661377
77 j 0.21134708
87 c 0.16602335
92 c 0.02175573
96 j 0.97864088

Andy

> From:  Ghislain Vieilledent
> 
> 
> Hi and sorry to disturb,
> 
> I'll try to be as clear as possible:
> I want to select rows of a data frame called "Data2.Iso" 
> regarding the 
> frequency of a factor variable called "Variete" that I want ">=4".
> 
> I used function table to have the frequency:
>  > FRAMEVARIETE<-as.data.frame(table(Data2.Iso$Variete))
> Then I selected the modalities with a frequency >=4:
>  > FRAMEVARIETE2<-FRAMEVARIETE[FRAMEVARIETE$Freq>=4,]
>  > as.character(FRAMEVARIETE2[,"Variete"])
> 
> But then, how to select the rows with those modalities?
> Does anyone can help me?
> 
> Thanks!
> 
> Ghislain.
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
> 
>




More information about the R-help mailing list