Subsection Data Frame

I have a simple question that I think. In my framework, I would like to make a subset where the Quality_score column is: Perfect, Perfect * , Perfect * , Good, Good ** and Good ***

This is in my solution:

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

Is there any way to simplify this method? How:

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

Thanks everyone

Lisanne

+3
source share
2 answers

You don’t even need subsetto check:?"["

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

EDITED: based on a comment of the form @Sacha Epskamp: ==in the expression gives incorrect results, therefore corrected it above to %in%. Thank!

Example 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
+2
source

, : grepl, , . | , OR, ignore.case :

methods<-c('Perfect', 'Perfect*', 'Perfect*', 'Good', 'Good','Good*')

completefile <- data.frame( Quality_score = c( methods, "bad", "terrible", "abbysmal"), foo = 1)

subset(completefile,grepl("good|perfect",Quality_score,ignore.case=TRUE))
1       Perfect   1
2      Perfect*   1
3      Perfect*   1
4          Good   1
5          Good   1
6         Good*   1

EDIT: , , ! :

subset(completefile,grepl("Good|Perfect",Quality_score))
+1

All Articles