How to combine all list items with another data table, provided

I have the following data:

library(data.table) dt1 <- data.table(var1 = c("wk1","wk1","wk2"), var2 = c(1,2,3)) dt2 <- data.table(var3 = c("a","b","c"), var2 = c(1,2,3)) lista <- list(dt1,dt2) dt_main <- data.table(var1 = c("wk1","wk2"), var4 = c(100,200)) 

I want to merge all the lista elements that contain the var1 variable with dt_main data.table , so at the end I would like lista look like this:

 dt1 <- data.table(var1 = c("wk1","wk1","wk2"), var2 = c(1,2,3), var4 = c(100,100,200)) dt2 <- data.table(var3 = c("a","b","c"), var2 = c(1,2,3)) lista <- list(dt1,dt2) 

I tried

 mapply(function(X,Y){ if("var1"%in%names(X)){ X <- merge(X,Y,by="var1") } },X=lista,Y=dt_main) 

but that will not work. Any help?

+5
source share
2 answers

You can use lapply and combine functions inside:

 lapply(lista, function(x) if (!is.null(x$var1)) { #the function checks if there is a var1 column #and if there is, it gets merged to the x data.table return(merge(dt_main, x, by = 'var1', all.x = TRUE)) } else { #otherwise it just returns the data.table return(x) }) # [[1]] # var1 var4 var2 # 1: wk1 100 1 # 2: wk1 100 2 # 3: wk2 200 3 # # [[2]] # var3 var2 # 1: a 1 # 2: b 2 # 3: c 3 
+4
source

A slightly different way to do this:

 lapply(lista, function(x) if ('var1' %in% names(x)) x[dt_main, on = 'var1', var4 := var4][] else x ) 

which gives:

 [[1]] var1 var2 var4 1: wk1 1 100 2: wk1 2 100 3: wk2 3 200 [[2]] var3 var2 1: a 1 2: b 2 3: c 3 
+4
source

Source: https://habr.com/ru/post/1273733/


All Articles