我正在研究一个与此类似的数据框:
ID = c("1","1","1","1","1","1","1","2","2","2","2","2","2","2","2","2","2","2","2","2","2")
TIME = c("0", "0.5", "1","1.5","2","2.5","3","0","0", "0.5","0.5", "1","1","1.5","1.5","2","2","2.5","2.5","3","3")
OBS = c("0", "0.73", "0.98", "1.24", "2.06","2.56","4.01", "0", "0.03", "0.76", "0.85", "2.13","2.78","3.9", "4.1", "5.4", "5.6", "7.8", "8.0","8.4","8.8")
VISITNUM = c("1","1","1","1","1","1","1","1","2", "1","2","1","2","1","2","1","2","1","2","1","2")
DF = data.frame(ID, TIME, OBS, VISITNUM)
我在订购数据框时遇到问题 - 基本上我想按以下时间顺序订购数据:
TIMEORDERED <- c("0","0.5","1","1.5","2","2","2.5","3")
给出这个
ID = c("1","1","1","1","1","1","1","2","2","2","2","2","2","2","2","2","2","2","2","2","2")
TIME = c("0", "0.5", "1","1.5","2","2.5","3","0", "0.5", "1","1.5","2","2.5","3","0", "0.5", "1","1.5","2","2.5","3")
OBS= c("0", "0.73", "0.98", "1.24", "2.06","2.56","4.01", "0", "0.76", "2.13","3.9", "5.4", "7.8", "8.4", "0.03", "0.85", "2.78", "4.1","5.6","8.0","8.8")
VISITNUM = c("1","1","1","1","1","1","1","1","1","1","1","1","1","1","2","2","2","2","2","2","2")
DFIWANT = data.frame(ID, TIME, OBS, VISITNUM)
问题是对同一主题进行了测量(DF $ ID = 2)(但访问次数不同,请参阅DF $ VISITNUM)。
我怀疑plyr包中的order()函数或arrange()函数是我的朋友,但我的尝试并不富有成效。
任何帮助都将不胜感激。
Sincerily ÿ
答案 0 :(得分:0)
是的,您可以使用order()
执行此操作,只需指定您希望数据框排序的变量:
DF.ordered <- DF[order(DF$ID, DF$VISITNUM, DF$TIME),]
你也可以使用plyr的arrange()
:
library(plyr)
DF.arranged <- arrange(DF, ID, VISITNUM, TIME)