I would like to do a row-wise sort using specific columns but also retain all columns from the original df.
Data:
df <- structure(list(C1 = c("ABC", "XYZ", "DEF"),
C2 = c("ZLO", "BCD", "PQR"),
C3 = c("E1", "E2", "E3")),
class = "data.frame", row.names = c(NA, -3L))
Desired output:
C1 C2 C3
ABC ZLO E1
BCD XYZ E2
DEF PQR E3
I tried to do this using:
df <- t(apply(df[1:2], 1,
FUN=function(x) sort(x, decreasing=FALSE)))
but it only returns the first two columns and I need help to: a) vectorize it b) retain all columns