Reputation: 515
Hi I have a data frame consists of single column which needs to be sorted on data values and not by column
col = c("zyx","cad","nm,po,qr","ba,dc,uu")
df = data.frame(col)
need to get output column as follows
col_out =c("xyz","acd","mn,op,qr","ab,cd,uu")
df = data.frame(col,col_out)
I have tried with "Order" statement but unable to get desired output.
Upvotes: 2
Views: 55
Reputation: 388982
Here is a base R option first splitting string on comma, then splitting them on every character and finally combining them in one string again.
sapply(strsplit(df$col, ','), function(x)
toString(sapply(strsplit(x, ''), function(y)
paste0(sort(y), collapse = ''))))
#[1] "xyz" "acd" "mn, op, qr" "ab, cd, uu"
Upvotes: 3