Row-wise sort then concatenate across specific columns of data frame

后端 未结 2 1928
悲&欢浪女
悲&欢浪女 2021-01-05 01:17

(Related question that does not include sorting. It\'s easy to just use paste when you don\'t need to sort.)

I have a less-than-ideally-structured tabl

2条回答
  •  情歌与酒
    2021-01-05 01:52

    My first thought would've been to do this:

    dt[, new_var := paste(sort(.SD), collapse = ", "), by = 1:nrow(dt)]
    

    But you could make your function work with a couple of simple modifications:

    f = function(...) paste(c(...)[order(c(...))],collapse=", ")
    
    dt[, new_var := do.call(function(...) mapply(f, ...), .SD)]
    

提交回复
热议问题