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

后端 未结 2 1923
悲&欢浪女
悲&欢浪女 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:46

    Just apply down rows:

    apply(df,1,function(x){
      paste(sort(x),collapse = ",")
    })
    

    Wrap it in a function if you want. You'll either have to define which columns to send or assume all. i.e. apply(df[ ,2:3],1,f()...

    sort(x) is the same as x[order(x)]

提交回复
热议问题