I have a datatable
as below:
c1 <- c("a","e","b","f")
c2 <- c("b","a","a","c")
c3 <- c("x","x","x","x")
df <- data.table(c1, c2)
Trying to get a result that looks like this:
c1 c2 new
1: a b ab
2: e a ae
3: b a ab
4: f c cf
I need the elements to be sorted before being concatenated as shown above
df[,new:=paste0(c1,c2)]
works as expected but when I try to sort using df[,new:=paste0(sort(c1,c2))]
, there is a problem.
Also, in some cases, it seems that when one adds multiple functions (like sort()
within paste()
), the entire column is provided i.e. the operations are not strictly row-wise.
I tried concatenating first and then splitting the string to reorder. It did not work.
Thankyou