我想按多列对data.frame进行排序.例如,对于下面的data.frame,我想按列z(降序)然后按列b(升序)排序:
dd <- data.frame(b = factor(c("Hi", "Med", "Hi", "Low"),
levels = c("Low", "Med", "Hi"), ordered = TRUE),
x = c("A", "D", "A", "C"), y = c(8, 3, 9, 9),
z = c(1, 1, 1, 2))
dd
b x y z
1 Hi A 8 1
2 Med D 3 1
3 Hi A 9 1
4 Low C 9 2
Run Code Online (Sandbox Code Playgroud) 这是我的数据的一个小的可重现的例子:
> mydata <- structure(list(subject = c(1, 1, 1, 2, 2, 2), time = c(0, 1, 2, 0, 1, 2), measure = c(10, 12, 8, 7, 0, 0)), .Names = c("subject", "time", "measure"), row.names = c(NA, -6L), class = "data.frame")
> mydata
subject time measure
1 0 10
1 1 12
1 2 8
2 0 7
2 1 0
2 2 0
Run Code Online (Sandbox Code Playgroud)
我想生成一个包含该measure特定主题的平均值的新变量,所以:
subject time measure mn_measure
1 0 10 10
1 1 12 10
1 2 …Run Code Online (Sandbox Code Playgroud)