I have a list of data.tables that I need to bind, however I only need the last X columns.
My data is structured as follows:
DT.1 <- data.table(x=c(1,1), y = c("a","a"), v1 = c(1,2), v2 = c(3,4)) DT.2 <- data.table(x=c(1,1), y = c("a","a"), v3 = c(5,6)) DT.3 <- data.table(x=c(1,1), y = c("a","a"), v4 = c(7,8), v5 = c(9,10), v6 = c(11,12)) DT.list <- list(DT.1, DT.2, DT.3) >DT.list [[1]] xy v1 v2 1: 1 a 1 3 2: 1 a 2 4 [[2]] xy v3 1: 1 a 5 2: 1 a 6 [[3]] xy v4 v5 v6 1: 1 a 7 9 11 2: 1 a 8 10 12
The columns x and y are the same for each of the data.tables, but the number of columns is different. The output should not include repeating columns x and y. It should look like this:
xy v1 v2 v3 v4 v5 v6 1: 1 a 1 3 5 7 9 11 2: 1 a 2 4 6 8 10 12
I want to avoid using a loop. I can bind data.tables using do.call("cbind", DT.list) and then delete duplicates manually, but is there a way when duplicates are not created in the first place? In addition, efficiency is important because lists can be long with big data. Tables.
thanks