【发布时间】:2020-02-24 16:17:43
【问题描述】:
我想使用特定列进行逐行排序,但也保留原始 df 中的所有列。
数据:
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
我尝试使用:
df <- t(apply(df[1:2], 1,
FUN=function(x) sort(x, decreasing=FALSE)))
但它只返回前两列,我需要帮助: a) 向量化它 b) 保留所有列
【问题讨论】:
标签: r sorting vectorization