# Find the value in an R data frame column that exist n times.

To find the value in an R data frame column that exist n times, we first need to tabulate the column with factor then extracting the levels of the column after that reading them with as.numeric.

Check out the Examples given below to understand how it can be done.

## Example 1

Following snippet creates a sample data frame −

x<-rpois(20,1)
df1<-data.frame(x)
df1

The following dataframe is created

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

To find the value in column x of df1 that exist 7 times on the above created data frame, add the following code to the above snippet −

x<-rpois(20,1)
df1<-data.frame(x)
as.numeric(levels(factor(df1$x))[tabulate(factor(df1$x))==7])

## Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 1

## Example 2

Following snippet creates a sample data frame −

y<-sample(0:5,20,replace=TRUE)
df2<-data.frame(y)
df2

The following dataframe is created

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

To find the value in column y of df2 that exist 2 times on the above created data frame, add the following code to the above snippet −

y<-sample(0:5,20,replace=TRUE)
df2<-data.frame(y)
as.numeric(levels(factor(df2$y))[tabulate(factor(df2$y))==2])

## Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 3

## Example 3

Following snippet creates a sample data frame −

z<-round(rnorm(20),0)
df3<-data.frame(z)
df3

The following dataframe is created

   z
1  -1
2   2
3  -1
4   1
5   1
6  -2
7  -1
8  -1
9   0
10  0
11  1
12  1
13 -1
14  0
15  1
16  0
17  0
18  0
19 -1
20  0

To find the value in column z of df3 that exist 6 times on the above created data frame, add the following code to the above snippet −

z<-round(rnorm(20),0)
df3<-data.frame(z)
as.numeric(levels(factor(df3$z))[tabulate(factor(df3$z))==6])

## Output

If you execute all the above given snippets as a single program, it generates the following Output −

numeric(0)