Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subselection dataframe

I have a simple questioon I think. In my dataframe I would like to make subset where column Quality_score is equal to: Perfect, Perfect*, Perfect*, Good, Good** and Good***

This in my solution by now:

>Quality_scoreComplete <- subset(completefile,Quality_score == "Perfect" | Quality_score=="Perfect***" | Quality_score=="Perfect****" | Quality_score=="Good" | Quality_score=="Good***" | Quality_score=="Good****") 

Is there a way to simplify this method? Like:

methods<-c('Perfect', 'Perfect***', 'Perfect****', 'Good', 'Good***','Good***')
Quality_scoreComplete <- subset(completefile,Quality_score==methods)

Thank you all,

Lisanne

like image 207
Samantha Avatar asked Dec 02 '25 18:12

Samantha


1 Answers

You do not even need subset, check: ?"["

Quality_scoreComplete <- completefile[completefile$Quality_score %in% methods,]

EDITED: based on kind comment of @Sacha Epskamp: == in the expression gives wrong results, so corrected it above to %in%. Thanks!

Example of the problem:

> x <- c(17, 19)
> cars[cars$speed==x,]
   speed dist
29    17   32
31    17   50
36    19   36
38    19   68
> cars[cars$speed %in% x,]
   speed dist
29    17   32
30    17   40
31    17   50
36    19   36
37    19   46
38    19   68
like image 95
daroczig Avatar answered Dec 05 '25 11:12

daroczig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!