
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Extract Columns with Non-Duplicate Values in R
To extract columns from an R data frame having at least one non-duplicate, we can use Filter function.
For Example, if we have a data frame called df that contains some columns having duplicate values and columns that do not contains at least one non-duplicate then we can extract columns having at least one non-duplicate by using the below command −
Filter(var,df)
Example 1
Following snippet creates a sample data frame −
x1<-rep(10,20) x2<-rpois(20,1) x3<-rpois(20,5) x4<-rep(1,20) df1<-data.frame(x1,x2,x3,x4) df1
The following dataframe is created
x1 x2 x3 x4 1 10 1 2 1 2 10 1 3 1 3 10 1 7 1 4 10 2 5 1 5 10 0 2 1 6 10 1 6 1 7 10 0 7 1 8 10 0 6 1 9 10 0 3 1 10 10 0 6 1 11 10 0 9 1 12 10 0 7 1 13 10 0 6 1 14 10 1 3 1 15 10 1 5 1 16 10 1 5 1 17 10 2 8 1 18 10 0 6 1 19 10 1 1 1 20 10 1 4 1
To extracting columns of df1 having at least one non-duplicate on the above created data frame, add the following code to the above snippet −
x1<-rep(10,20) x2<-rpois(20,1) x3<-rpois(20,5) x4<-rep(1,20) df1<-data.frame(x1,x2,x3,x4) Filter(var,df1)
Output
If you execute all the above given snippets as a single program, it generates the following Output −
x2 x3 1 1 2 2 1 3 3 1 7 4 2 5 5 0 2 6 1 6 7 0 7 8 0 6 9 0 3 10 0 6 11 0 9 12 0 7 13 0 6 14 1 3 15 1 5 16 1 5 17 2 8 18 0 6 19 1 1 20 1 4
Example 2
Following snippet creates a sample data frame −
y1<-round(rnorm(20),1) y2<-round(rnorm(20),1) y3<-rep(0.5,20) y4<-rep(0.8,20) df2<-data.frame(y1,y2,y3,y4) df2
The following dataframe is created
y1 y2 y3 y4 1 0.7 0.3 0.5 0.8 2 0.4 -1.7 0.5 0.8 3 -0.7 -0.3 0.5 0.8 4 0.5 -1.4 0.5 0.8 5 0.7 -0.8 0.5 0.8 6 0.3 0.4 0.5 0.8 7 -0.8 0.5 0.5 0.8 8 0.3 0.5 0.5 0.8 9 0.4 -0.1 0.5 0.8 10 0.6 -0.3 0.5 0.8 11 -0.8 -1.3 0.5 0.8 12 0.6 -0.9 0.5 0.8 13 2.4 1.0 0.5 0.8 14 -0.6 0.4 0.5 0.8 15 1.8 0.9 0.5 0.8 16 -1.4 0.6 0.5 0.8 17 0.6 -0.1 0.5 0.8 18 0.6 0.7 0.5 0.8 19 0.5 -1.2 0.5 0.8 20 -1.0 0.7 0.5 0.8
To extract columns of df2 having at least one non-duplicate on the above created data frame, add the following code to the above snippet −
y1<-round(rnorm(20),1) y2<-round(rnorm(20),1) y3<-rep(0.5,20) y4<-rep(0.8,20) df2<-data.frame(y1,y2,y3,y4) Filter(var,df2)
Output
If you execute all the above given snippets as a single program, it generates the following Output −
y1 y2 1 0.7 0.3 2 0.4 -1.7 3 -0.7 -0.3 4 0.5 -1.4 5 0.7 -0.8 6 0.3 0.4 7 -0.8 0.5 8 0.3 0.5 9 0.4 -0.1 10 0.6 -0.3 11 -0.8 -1.3 12 0.6 -0.9 13 2.4 1.0 14 -0.6 0.4 15 1.8 0.9 16 -1.4 0.6 17 0.6 -0.1 18 0.6 0.7 19 0.5 -1.2 20 -1.0 0.7