Merge multiple data tables with duplicate column names

后端 未结 6 1417
感情败类
感情败类 2020-12-29 10:19

I am trying to merge (join) multiple data tables (obtained with fread from 5 csv files) to form a single data table. I get an error when I try to merge 5 data tables, but wo

6条回答
  •  太阳男子
    2020-12-29 10:41

    Using reshaping gives you a lot more flexibility in how you want to name your columns.

    library(dplyr)
    library(tidyr)
    
    list(DT1, DT2, DT3, DT4, DT5) %>%
      bind_rows(.id = "source") %>%
      mutate(source = paste("y", source, sep = ".")) %>%
      spread(source, y)
    

    Or, this would work

    library(dplyr)
    library(tidyr)
    
    list(DT1 = DT1, DT2 = DT2, DT3 = DT3, DT4 = DT4, DT5 = DT5) %>%
      bind_rows(.id = "source") %>%
      mutate(source = paste(source, "y", sep = ".")) %>%
      spread(source, y)
    

提交回复
热议问题