data table string concatenation of SD columns for by group values

后端 未结 1 1155
不思量自难忘°
不思量自难忘° 2021-01-12 17:08

I have a big data set with many variables that looks similar to this :

 > data.table(a=letters[1:10],b=LETTERS[1:10],ID=c(1,1,1,2,2,2,2,3,3,3))
     a b          


        
相关标签:
1条回答
  • 2021-01-12 17:48

    You can concatenate all columns in using lapply.

    dt[, lapply(.SD, paste0, collapse=" "), by = ID]
    ##    ID       a       b
    ## 1:  1   a b c   A B C
    ## 2:  2 d e f g D E F G
    ## 3:  3   h i j   H I J
    

    Using newline characters as a ollapse argument instead of " " does work, but does not print as you seem to expect in your desired output.

    dt[, lapply(.SD, paste0, collapse="\n"), by = ID]
    ##    ID          a          b
    ## 1:  1    a\nb\nc    A\nB\nC
    ## 2:  2 d\ne\nf\ng D\nE\nF\nG
    ## 3:  3    h\ni\nj    H\nI\nJ
    

    As pointed out in the comments by @Frank, the question has been changed to have , as a seperator instead of \n. Of course you can just change the collapse argument to ",". If you want to have a space as well ", ", then the solution by @DavidArenburg is preferable.

    dt[, lapply(.SD, paste0, collapse=","), by = ID]
    dt[, lapply(.SD, toString), by = ID]
    
    0 讨论(0)
提交回复
热议问题