# How to subset an R data frame by ignoring a value in one of the columns?

To subset an R data frame by ignoring a value in one of the columns, we can follow the below steps −

• First of all, create a data frame.

• Then, use single square brackets to subset the data frame by ignoring a value in one of the columns.

## Example

#### Create the data frame

Let’s create a data frame as shown below −

x<-rpois(30,5)
y<-rpois(30,5)
z<-rpois(30,2)
df<-data.frame(x,y,z)
df

## Output

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

   x y  z
1  2 10 3
2  5  3 2
3  2  5 0
4  5  5 1
5  5  6 1
6  4  8 1
7  4  7 5
8  7  7 2
9  5  4 0
10 7  6 2
11 4  2 2
12 6  4 4
13 9  2 1
14 9  2 1
15 4  1 2
16 8  3 1
17 4  3 1
18 6  5 5
19 3  4 2
20 3  8 2
21 6  1 4
22 6 11 1
23 5  6 2
24 4  5 0
25 3  7 2
26 6  5 1
27 6  6 0
28 2  5 2
29 5  3 1
30 5  4 2

Subset the data frame by ignoring a value in one of the columns

Using single square brackets to subset the data frame df by ignoring 2 in column z as shown below −

x<-rpois(30,5)
y<-rpois(30,5)
z<-rpois(30,2)
df<-data.frame(x,y,z)
df[df\$z!=2,]

## Output

   x  y z
1  2 10 3
3  2  5 0
4  5  5 1
5  5  6 1
6  4  8 1
7  4  7 5
9  5  4 0
12 6  4 4
13 9  2 1
14 9  2 1
16 8  3 1
17 4  3 1
18 6  5 5
21 6  1 4
22 6 11 1
24 4  5 0
26 6  5 1
27 6  6 0
29 5  3 1

Updated on: 12-Nov-2021

579 Views