I have data in the following form:
set.seed(1234) data <- data.frame(cbind(runif(40,0,10), rep(seq(1,20,1), each = 2))) data <- data[sample(nrow(data)),] colnames(data) <- c("obs","subject") head(data) obs subject 1.5904600 12 8.1059855 13 5.4497484 6 0.3999592 12 2.5880982 19 2.6682078 9 ... ...
Let's say that I have only two observations (column "obs") on the subject (column "subject", where the subjects are numbered from 1 to 20).
I would like to "group" the rows by the values โโof the "subject" column. More precisely, I would like to "order" data on the topic, but preserving the order shown above. Thus, the final data will be something like this:
obs subject 1.5904600 12 0.3999592 12 8.1059855 13 2.3656473 13 5.4497484 6 7.2934746 6
Any ideas? I was thinking about what would probably identify each line corresponding to the subject, with which :
which(data$subject==x)
and then rbind these lines in a loop, but I'm sure there is a simpler and faster way to do this, right?
source share