akh22
akh22

Reputation: 701

Data Table R: Merge selected columns from multiple data.table

I have hundreds of data tables like an example given below that have the exact same of rows and columns. I would like to merge(not by rbindlist) selected columns (e.g. ID, FDR and LogFC) from all the data tables into an one combined data.table.

               ID         P        FDR      LogFC            animals
 1:            A    1.568780e-01 2.5e-01  0.3348365           dog
 2:            B    4.960651e-05 2.6e-04  0.5768812           dog
 3:            C    3.123122e-01 4.2e-01  0.1872491           dog
 4:            D    8.583002e-07 8.3e-06  0.8387016           dog
 5:            E    2.776600e-03 8.4e-03  0.6495237           dog
 6:            F    3.925375e-06 3.3e-05  1.2510581           cat
 7:            G    4.248726e-07 4.7e-06  1.4029001           cat
 8:            H    1.497479e-03 4.8e-03  0.7471876           cat
 9:            I    3.335468e-07 4.0e-06  1.2297170           cat
10:            J    5.889612e-04 2.2e-03  1.0751952           cat

I tried using a following (from https://stackoverflow.com/posts/39561045/revisions;)

DT.comb<-Reduce(function(...) merge.data.table (..., by="ID", all = TRUE), dt.list, allow.cartesian=T)

but I am not sure how to pass column selections to merge.data.table. Although I can achieve this by using rbindlist, delete unwanted columns (e.g. P and animals) and then dcast the table to convert it to a wide format, I will be great if I can use merge.data.table, or other approach, if possible. I'd really appreciate any input on this.

Upvotes: 1

Views: 629

Answers (1)

akrun
akrun

Reputation: 887691

Just change the by = "ID" to by = c("ID", "FDR", "logFC") and the argument allow.cartesian should be inside the merge

DT.comb <- Reduce(function(...) merge.data.table(...,
   by= c("ID", "FDR", "LogFC"),  all = TRUE,  allow.cartesian=TRUE), dt.list)

Upvotes: 2

Related Questions