How to subset an R data frame if one of the supplied grouping values is found?


Subsetting is one of the commonly used technique which serves many different purposes depending on the objective of analysis. To subset a data frame if one of the supplied grouping values is found means that we want to subset if any of the categorical variable values is present in the categorical column, for this purpose we can follow the below steps −

  • Creating a data frame.
  • Subsetting the data frame if any of the supplied value of categorical variable exist.

Create the data frame

Let's create a data frame as shown below −

 Live Demo

x<-rnorm(20,2)
Grp<-sample(LETTERS[1:4],20,replace=TRUE)
df<-data.frame(x,Grp)
df

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

      x    Grp
1 0.3359965 A
2 2.0416162 A
3 0.9818926 A
4 1.4677398 D
5 1.5241767 B
6 1.3522920 A
7 0.1696135 D
8 2.8538156 C
9 1.5310543 C
10 3.8808002 A
11 1.6601963 C
12 0.7691347 A
13 -0.3206033 B
14 3.0502846 A
15 2.7044244 C
16 0.4451980 A
17 0.8252894 C
18 0.8465413 A
19 2.0163445 C
20 1.5913034 C

Subsetting the data frame

Loading dplyr package and subsetting df if A or C exists in Grp column −

library(dplyr)
x<-rnorm(20,2)
Grp<-sample(LETTERS[1:4],20,replace=TRUE)
df<-data.frame(x,Grp)
df %>% filter(Grp=="A"|Grp=="C")

Output

      x    Grp
1 0.3359965 A
2 2.0416162 A
3 0.9818926 A
4 1.3522920 A
5 2.8538156 C
6 1.5310543 C
7 3.8808002 A
8 1.6601963 C
9 0.7691347 A
10 3.0502846 A
11 2.7044244 C
12 0.4451980 A
13 0.8252894 C
14 0.8465413 A
15 2.0163445 C
16 1.5913034 C

Updated on: 13-Aug-2021

22 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements