concatenate values across columns in data.table, row by row

前端 未结 1 1947
清酒与你
清酒与你 2021-01-17 16:25

I have data.table like this:

x <- data.table(
       a = c( 1,     2),     
       b = c(\'foo\', \'bar\'))

I want to add a new column \

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

    You can use do.call(), using .SDcols to supply the columns.

    x[, key_ := do.call(paste, c(.SD, sep = "_")), .SDcols = names(x)]
    

    .SDcols = names(x) supplies all the columns of x. You can supply any vector of names or column numbers there.

    0 讨论(0)
提交回复
热议问题