How to filter column values for some strings from an R data frame using dplyr?

R ProgrammingServer Side ProgrammingProgramming

Filtering data helps us to make desired groups of data than can be further used for analysis. In this way, accuracy can be achieved and computation becomes easy. Suppose, we have a homogeneous group then to partition that group based on some characteristics the filter function of dplyr package can be used.

Example

Consider the below data frame −

> Subject<-rep(c("Stats","Physics","Chemistry","Bio","IT","Marketing"),
+ times=c(5,8,7,6,9,5))
> Score<-sample(1:100,40,replace=TRUE)
> df<-data.frame(Subject,Score)
> head(df,20)
 Subject Score
1 Stats 88
2 Stats 20
3 Stats 49
4 Stats 31
5 Stats 83
6 Physics 29
7 Physics 43
8 Physics 73
9 Physics 28
10 Physics 74
11 Physics 93
12 Physics 42
13 Physics 73
14 Chemistry 29
15 Chemistry 53
16 Chemistry 70
17 Chemistry 42
18 Chemistry 99
19 Chemistry 10
20 Chemistry 28

Loading dplyr package −

> library(dplyr)

Now suppose we want to filter subjects as shown below −

> Subject1<-c("Physics","Chemistry")
> df %>% filter(Subject %in% Subject1)
    Subject Score
 1   Physics 29
 2   Physics 43
 3   Physics 73
 4   Physics 28
 5   Physics 74
 6   Physics 93
 7   Physics 42
 8   Physics 73
 9 Chemistry 29
10 Chemistry 53
11 Chemistry 70
12 Chemistry 42
13 Chemistry 99
14 Chemistry 10
15 Chemistry 28
> Subject2<-c("Stats","Marketing","IT")
> df %>% filter(Subject %in% Subject2)
     Subject Score
 1     Stats 88
 2     Stats 20
 3     Stats 49
 4     Stats 31
 5     Stats 83
 6        IT 26
 7        IT 70
 8        IT 71
 9        IT 74
10        IT 10
11        IT 8
12        IT 42
13        IT 62
14        IT 90
15 Marketing 41
16 Marketing 39
17 Marketing 66
18 Marketing 4
19 Marketing 96
> Subject3<-c("Bio")
> df %>% filter(Subject %in% Subject3)
Subject Score
1 Bio 82
2 Bio 96
3 Bio 25
4 Bio 61
5 Bio 47
6 Bio 95
raja
Published on 12-Aug-2020 09:59:49
Advertisements