How to create a subset of an R data frame based on multiple columns?

R ProgrammingServer Side ProgrammingProgramming

To create a subset of an R data frame based on multiple columns, we can follow the below steps −

  • First of all, create a data frame.
  • Then, use single square brackets to subset the data frame based on multiple columns.

Create the data frame

Let's create a data frame as shown below −

 Live Demo

x1<-sample(1:5,20,replace=TRUE)
x2<-sample(1:5,20,replace=TRUE)
x3<-sample(1:5,20,replace=TRUE)
df<-data.frame(x1,x2,x3)
df

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

  x1 x2 x3
1 5 3  5
2 5 4 3
3 1 5 5
4 2 2 4
5 4 4 4
6 4 1 3
7 5 2 3
8 4 5 1
9 5 3 4
10 4 2 2
11 5 3 1
12 4 4 5
13 3 1 3
14 1 2 3
15 3 2 4
16 2 3 3
17 3 2 5
18 5 5 2
19 1 2 3
20 1 5 1

Find the subset based on multiple columns

Using single square brackets to subset df with column 1 or column2 or column 3 having a value equal to 1 −

 Live Demo

x1<-sample(1:5,20,replace=TRUE)
x2<-sample(1:5,20,replace=TRUE)
x3<-sample(1:5,20,replace=TRUE)
df<-data.frame(x1,x2,x3)
df[(df[,1]==1|df[,2]==1|df[,3]==1),]

Output

  x1 x2 x3
 3 1 5 5
 6 4 1 3
 8 4 5 1
11 5 3 1
13 3 1 3
14 1 2 3
19 1 2 3
20 1 5 1
raja
Published on 04-Aug-2021 11:22:56
Advertisements