r - How to conditionally combine data.frame object in the list in more elegant way? -


i have data.frame in list, , intend merge specific data.frame objects conditionally merge second, third data.frame objects without duplication, merge first data.frame objects. however, used rbind function task, approach not elegant. can me out improve solution ? how can achieve more compatible solution can used in dynamic functional programming ? how can desired output ? idea ?

reproducible example:

dflist <- list(   df.1 = data.frame(red=c(1,2,3), blue=c(na,1,2), green=c(1,1,2)),   df.2 = data.frame(red=c(2,3,na), blue=c(1,2,3), green=c(1,2,4)),   df.3 = data.frame(red=c(2,3,na,na), blue=c(1,2,na,3), green=c(1,2,3,4)) ) 

dummy way it:

rbind(dflist[[1l]], unique(rbind(dflist[[2l]], dflist[[3l]]))) 

apparently, attempt not elegant apply in functional programming. how can make happen elegantly ?

desired output :

red blue green 1    1   na     1 2    2    1     1 3    3    2     2 11   2    1     1 21   3    2     2 31  na    3     4 6   na   na     3 

how can improve solution more elegantly , efficiently ? in advance

the best (easiest , fastest way) data.table::rbindlist.

it work this:

library(data.table) dflist <- list(   df.1 = data.table(red=c(1,2,3), blue=c(na,1,2), green=c(1,1,2)),   df.2 = data.table(red=c(2,3,na), blue=c(1,2,3), green=c(1,2,4)),   df.3 = data.table(red=c(2,3,na,na), blue=c(1,2,na,3), green=c(1,2,3,4)) )  # part 1: list element 1 dt_1 <- dflist[[1]]  # part 2: other list elements (in case 2 , 3) dt_2 <- unique(rbindlist(dflist[-1]))  # use rbindlist bind rows dt_all <- rbindlist(list(dt_1, dt_2)) 

comment.

my solution pretty close proposed solution. think "ugliness" way is edge case merge datasets , deattach first element (and treat in different way). best solution step , think underlying idea , solve using additional variable in datasets (i.e., df1 , df2_3), consider r-way.

something along thought this:

mylist2 <- list(   df.1 = data.table(red=c(1,2,3), blue=c(na,1,2), green=c(1,1,2), var = "df1"),   df.2 = data.table(red=c(2,3,na), blue=c(1,2,3), green=c(1,2,4), var = "other"),   df.3 = data.table(red=c(2,3,na,na), blue=c(1,2,na,3), green=c(1,2,3,4), var = "other") )  dt <- rbindlist(mylist2) unique(dt) # red blue green   var # 1:   1   na     1   df1 # 2:   2    1     1   df1 # 3:   3    2     2   df1 # 4:   2    1     1 other # 5:   3    2     2 other # 6:  na    3     4 other # 7:  na   na     3 other 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -