I already have an id variable (patient_id), and I have multiple observations per id (blood_pressure), but I want a new id variable containing 1 for the first id, 2 for the second, and so on within each patient_id. Please see my interested data format in the link. How can I generate “order” variable using R? I want something like this:
Asked
Active
Viewed 70 times
1 Answers
0
Here's a solution that uses dplyr
. First, I create a toy data frame. (For future questions, it's easier for us if you post copy-and-pasteable data rather than an image. dput
is useful for this.)
df <- data.frame(patient_id = c(1, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 4),
blood_pressure = sample(90:134, 12, replace = TRUE))
which looks like this:
# patient_id blood_pressure
# 1 1 130
# 2 1 130
# 3 1 128
# 4 2 109
# 5 2 91
# 6 3 95
# 7 3 129
# 8 4 134
# 9 4 116
# 10 4 101
# 11 4 131
# 12 4 112
Next, I load the required library.
library(dplyr)
Then, I group by patient_id
, add a variable called order
using the mutate
function, which is the row number within each group given by the row_number
function, and finally ungroup
the result.
df %>%
group_by(patient_id) %>%
mutate(order = row_number()) %>%
ungroup
giving,
# patient_id blood_pressure order
# <dbl> <int> <int>
# 1 1 130 1
# 2 1 130 2
# 3 1 128 3
# 4 2 109 1
# 5 2 91 2
# 6 3 95 1
# 7 3 129 2
# 8 4 134 1
# 9 4 116 2
# 10 4 101 3
# 11 4 131 4
# 12 4 112 5

Dan
- 11,370
- 4
- 43
- 68
-
@MiladNazarzadeh Please select this as the answer. – dimitriy Jan 11 '19 at 00:12