Question

I am working on a data frame similar to this one here:

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)

I am having trouble ordering the data frame - basically I want to order my data by the following time sequence:

TIMEORDERED <- c("0","0.5","1","1.5","2","2","2.5","3")

giving this

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) 

The issue is there are measurements on the same subject (DF$ID = 2) (yet different visits, see DF$VISITNUM).

I suspect the order() function or the arrange() function in the plyr package are my friends, yet my attempts have not been fruitful.

Any help would be appreciated.

Sincerily Y

Was it helpful?

Solution

Yes, you can do this with order(), you just need to specify the variables you want the dataframe ordered by:

DF.ordered <- DF[order(DF$ID, DF$VISITNUM, DF$TIME),]

You are also correct that you can use arrange() from plyr:

library(plyr)
DF.arranged <- arrange(DF, ID, VISITNUM, TIME)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top