我有一个问题。我想按每个ID的升序对TIME进行排序,我尝试了简单的方法,但它堆叠了所有ID的相同时间点。我想要单独的ID,时间按升序排序。提前致谢。
数据是
operator<
答案 0 :(得分:1)
data.table
您可以执行以下操作:
dt <- data.table(ID = c(1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2),
TIME = c(0,2,12,36,4,24,8,48,0,48,4,36,12,24,8,2),
EV = c(0,1024.88,1229.82,1269.47,1274.37,1274.37,1274.37,1274.37,
0,924.84,1356.87,1459.79,1467.43,1467.43,1467.43,1467.43))
dt[order(ID, TIME)]
ID TIME EV
1: 1 0 0.00
2: 1 2 1024.88
3: 1 4 1274.37
4: 1 8 1274.37
5: 1 12 1229.82
6: 1 24 1274.37
7: 1 36 1269.47
8: 1 48 1274.37
9: 2 0 0.00
10: 2 2 1467.43
11: 2 4 1356.87
12: 2 8 1467.43
13: 2 12 1467.43
14: 2 24 1467.43
15: 2 36 1459.79
16: 2 48 924.84
答案 1 :(得分:0)
您的问题无法重现(请参阅How to make a great R reproducible example?)。但是,使用dplyr
包是一个简单的解决方案。
df <- data.frame(ID = c(1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2),
TIME = c(0,2,12,36,4,24,8,48,0,48,4,36,12,24,8,2),
EV = c(0,1024.88,1229.82,1269.47,1274.37,1274.37,1274.37,1274.37,
0,924.84,1356.87,1459.79,1467.43,1467.43,1467.43,1467.43))
library(dplyr)
df %>%
arrange(ID, TIME)
ID TIME EV
1 0 0.00
1 2 1024.88
1 4 1274.37
1 8 1274.37
1 12 1229.82
1 24 1274.37
1 36 1269.47
1 48 1274.37
2 0 0.00
2 2 1467.43
2 4 1356.87
2 8 1467.43
2 12 1467.43
2 24 1467.43
2 36 1459.79
2 48 924.84
编辑:这不是您想要的吗?