
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
Remove Duplicates in Series from Each Row in an R Data Frame
To remove duplicates in series from each row in an R data frame, we can follow the below steps −
- Create a data frame.
- Removing duplicates in series from rows of the data frame
Create the data frame
Let's create a data frame as shown below −
x1<-rpois(20,1) x2<-rpois(20,1) x3<-rpois(20,1) x4<-rpois(20,1) df<-data.frame(x1,x2,x3,x4) df
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
x1 x2 x3 x4 1 0 1 1 0 2 0 0 0 0 3 0 1 2 2 4 3 1 1 0 5 0 1 1 2 6 0 1 0 1 7 1 0 1 1 8 0 0 0 1 9 0 0 0 2 10 2 0 0 1 11 0 0 0 3 12 0 1 1 2 13 0 3 2 0 14 2 1 0 2 15 0 0 0 1 16 0 0 0 3 17 1 1 2 0 18 1 0 0 0 19 1 0 1 3 20 0 0 2 1
Removing duplicates in series from rows of the data frame
Use apply function with rle function to remove duplicates in rows of df −
x1<-rpois(20,1) x2<-rpois(20,1) x3<-rpois(20,1) x4<-rpois(20,1) df<-data.frame(x1,x2,x3,x4) apply(df,1,FUN=function(x) rle(x)$values)
Output
[[1]] x1 x3 x4 0 1 0 [[2]] x4 0 [[3]] x1 x2 x4 0 1 2 [[4]] x1 x3 x4 3 1 0 [[5]] x1 x3 x4 0 1 2 [[6]] x1 x2 x3 x4 0 1 0 1 [[7]] x1 x2 x4 1 0 1 [[8]] x3 x4 0 1 [[9]] x3 x4 0 2 [[10]] x1 x3 x4 2 0 1 [[11]] x3 x4 0 3 [[12]] x1 x3 x4 0 1 2 [[13]] x1 x2 x3 x4 0 3 2 0 [[14]] x1 x2 x3 x4 2 1 0 2 [[15]] x3 x4 0 1 [[16]] x3 x4 0 3 [[17]] x2 x3 x4 1 2 0 [[18]] x1 x4 1 0 [[19]] x1 x2 x3 x4 1 0 1 3 [[20]] x2 x3 x4 0 2 1
Advertisements