Fast concatenation of data.table columns into one string column

后端 未结 3 1590
挽巷
挽巷 2021-01-31 18:35

Given an arbitrary list of column names in a data.table, I want to concatenate the contents of those columns into a single string stored in a new column. The column

3条回答
  •  一个人的身影
    2021-01-31 18:57

    This uses unite from package tidyr. May not be the fastest, but it is probably faster than hand-coded R code.

    library(tidyr)
    system.time(
      DNew <- DT %>% unite(State, ConcatCols, sep = "", remove = FALSE)
    )
    # user  system elapsed 
    # 14.974   0.183  15.343 
    
    DNew[1:10]
    # State   x   y a b c d e f
    # 1: foo211621bar foo bar 2 1 1 6 2 1
    # 2: foo532735bar foo bar 5 3 2 7 3 5
    # 3: foo965776bar foo bar 9 6 5 7 7 6
    # 4: foo221284bar foo bar 2 2 1 2 8 4
    # 5: foo485976bar foo bar 4 8 5 9 7 6
    # 6: foo566778bar foo bar 5 6 6 7 7 8
    # 7: foo892636bar foo bar 8 9 2 6 3 6
    # 8: foo836672bar foo bar 8 3 6 6 7 2
    # 9: foo963926bar foo bar 9 6 3 9 2 6
    # 10: foo385216bar foo bar 3 8 5 2 1 6
    

提交回复
热议问题