Reputation: 2297
I have a list that contain multiple data.frame with same name setting. I would like to combine them together in to one data.frame and adrop the duplicates. how can I do that?
sample data can be build using codes:
lst1 <- list(data1 = mpg, data2 = mpg, data3 = mpg)
The results will be mpg
.
sth like:
Many thanks.
Upvotes: 0
Views: 92
Reputation: 39595
Maybe this. You can create an id for each dataframe in the list and after binding you can exclude the duplicated values:
library(dplyr)
#Data
lst1 <- list(data1 = mpg, data2 = mpg, data3 = mpg)
lst1 <- lapply(lst1, function(x) {x$id<-1:nrow(x);return(x)})
#Bind
df <- do.call(bind_rows,lst1)
df2 <- df[!duplicated(df$id),]
df2$id <- NULL
Update: To keep data source:
library(dplyr)
#Data
lst1 <- list(data1 = mpg, data2 = mpg, data3 = mpg)
lst1 <- lapply(lst1, function(x) {x$id<-1:nrow(x);return(x)})
#Bind
df <- bind_rows(lst1,.id = 'data')
df2 <- df[!duplicated(df$id),]
df2$id <- NULL
Upvotes: 1