- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
- Related Articles
- How to find the most frequent factor value in an R data frame column?
- Find the column name that contains value greater than a desired value in each row of an R data frame.
- How to count the number of times a value occurs in a column of an R data frame?
- Create a quartile column for each value in an R data frame column.
- How to find the column index in an R data frame that matches a condition?
- Find the column name with the largest value for each row in an R data frame.
- Find the standard deviation for every n number of observations in an R data frame column.
- Change the decimal point of every value in an R data frame column.
- How to find the maximum value in an R data frame?
- How to find the number of times a variable changes its sign in an R data frame column?
- How to find mode for an R data frame column?
- How to find the row and column index of a character value in an R data frame?
- How to extract the first highest occurring value in an R data frame column?
- Find the number of non-missing values in each column by group in an R data frame.\n
- How to replace zero with previous value in an R data frame column?
