Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pair rows with the same value in one column of a dataframe in R

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") by subject (column "subject", where subjects are numbered from 1 to 20).

I would like to "group" rows by values of the "subject" column. More precisely, I would like to "order" data by subject, but conserving the order displayed above. Thus, final data would 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 thought of maybe identifying each row corresponding to a subject with which:

which(data$subject==x)

then rbind these rows in a loop but I am sure there is a simpler and faster way to do this, isn't it ?

like image 894
Ladislas Nalborczyk Avatar asked Nov 09 '22 08:11

Ladislas Nalborczyk


1 Answers

Convert to factor with levels then order:

data$group <- factor(data$subject, levels = unique(data$subject))
data[ order(data$group), ]

#           obs subject group
# 1  1.59046003      12    12
# 4  0.39995918      12    12
# 2  8.10598552      13    13
# 30 2.18799541      13    13
# ...
like image 135
zx8754 Avatar answered Nov 15 '22 06:11

zx8754