Home > OS >  dplyr::cur_group_id() by order of appearance rather than alphabetical order
dplyr::cur_group_id() by order of appearance rather than alphabetical order

Time:06-11

To a data which looks like as follows, I'm trying to add a column to record the trial order. In the data, reaction times (rts) were recorded in the order of trial; which means that participant p1 reacted to item i1, i2, and i3 in this order; participant p2 reacted to item i2, i1, and i3 in this order; then finally participant p3 reacted to i3 and i1 in this order.

library(tidyverse) # For dplyr, tibble, and purrr

data <- tribble(
  ~ p,  ~ r,  ~ i,  ~ rt,
  "p1", "r1", "i1", 1234,
  "p1", "r2", "i1", 1256,
  "p1", "r3", "i1", 1256,
  "p1", "r4", "i1", 1256,
  "p1", "qq", "i1", 1356,
  "p1", "r1", "i2", 1356,
  "p1", "r2", "i2", 1356,
  "p1", "r3", "i2", 1376,
  "p1", "qq", "i2", 1374,
  "p1", "r1", "i3", 1374,
  "p1", "r2", "i3", 1174,
  "p1", "r3", "i3", 1174,
  "p1", "qq", "i3", 1173,
  "p2", "r1", "i2", 1163,
  "p2", "r2", "i2", 1163,
  "p2", "r3", "i2", 1163,
  "p2", "qq", "i2", 1163,
  "p2", "r1", "i1", 1263,
  "p2", "r2", "i1", 1263,
  "p2", "r3", "i1", 1295,
  "p2", "r4", "i1", 1495,
  "p2", "qq", "i1", 1495,
  "p2", "r1", "i3", 1414,
  "p2", "r2", "i3", 1414,
  "p2", "r3", "i3", 1414,
  "p2", "qq", "i3", 1484,
  "p3", "r1", "i3", 1484,
  "p3", "r2", "i3", 1182,
  "p3", "r3", "i3", 1182,
  "p3", "qq", "i3", 1282,
  "p3", "r1", "i1", 1282,
  "p3", "r2", "i1", 1282,
  "p3", "r3", "i1", 1232,
  "p3", "r4", "i1", 1232,
  "p3", "qq", "i1", 1234
)

Therefore, I want to get a new data, by adding a column that indicates the trial order to the data above, as shown below:

| p    |  r   |  i   |  rt  | trial_order |
| "p1" | "r1" | "i1" | 1234 | 1 |
| "p1" | "r2" | "i1" | 1256 | 1 |
| "p1" | "r3" | "i1" | 1256 | 1 |
| "p1" | "r4" | "i1" | 1256 | 1 |
| "p1" | "qq" | "i1" | 1356 | 1 |
| "p1" | "r1" | "i2" | 1356 | 2 |
| "p1" | "r2" | "i2" | 1356 | 2 |
| "p1" | "r3" | "i2" | 1376 | 2 |
| "p1" | "qq" | "i2" | 1374 | 2 |
| "p1" | "r1" | "i3" | 1374 | 3 |
| "p1" | "r2" | "i3" | 1174 | 3 |
| "p1" | "r3" | "i3" | 1174 | 3 |
| "p1" | "qq" | "i3" | 1173 | 3 |
| "p2" | "r1" | "i2" | 1163 | 1 |
| "p2" | "r2" | "i2" | 1163 | 1 |
| "p2" | "r3" | "i2" | 1163 | 1 |
| "p2" | "qq" | "i2" | 1163 | 1 |
| "p2" | "r1" | "i1" | 1263 | 2 |
| "p2" | "r2" | "i1" | 1263 | 2 |
| "p2" | "r3" | "i1" | 1295 | 2 |
| "p2" | "r4" | "i1" | 1495 | 2 |
| "p2" | "qq" | "i1" | 1495 | 2 |
| "p2" | "r1" | "i3" | 1414 | 3 |
| "p2" | "r2" | "i3" | 1414 | 3 |
| "p2" | "r3" | "i3" | 1414 | 3 |
| "p2" | "qq" | "i3" | 1484 | 3 |
| "p3" | "r1" | "i3" | 1484 | 1 |
| "p3" | "r2" | "i3" | 1182 | 1 |
| "p3" | "r3" | "i3" | 1182 | 1 |
| "p3" | "qq" | "i3" | 1282 | 1 |
| "p3" | "r1" | "i1" | 1282 | 2 |
| "p3" | "r2" | "i1" | 1282 | 2 |
| "p3" | "r3" | "i1" | 1232 | 2 |
| "p3" | "r4" | "i1" | 1232 | 2 |
| "p3" | "qq" | "i1" | 1234 | 2 |

I tried to use dplyr::cur_group_id() following this and this, but I ended up with a problem that dplyr::cur_group_id() does not respect the order in which the items were presented to each participant. Rather, dplyr::cur_group_id() reflects the alphabetical order of items. What should I amend...?

# A tibble: 35 × 5
# Groups:   i [3]
   p     r     i        rt    ID
   <chr> <chr> <chr> <dbl> <int>
 1 p1    r1    i1     1234     1
 2 p1    r2    i1     1256     1
 3 p1    r3    i1     1256     1
 4 p1    r4    i1     1256     1
 5 p1    qq    i1     1356     1
 6 p1    r1    i2     1356     2
 7 p1    r2    i2     1356     2
 8 p1    r3    i2     1376     2
 9 p1    qq    i2     1374     2
10 p1    r1    i3     1374     3
11 p1    r2    i3     1174     3
12 p1    r3    i3     1174     3
13 p1    qq    i3     1173     3
14 p2    r1    i2     1163     2 # This should be 1
15 p2    r2    i2     1163     2 # This should be 1
16 p2    r3    i2     1163     2 # This should be 1
17 p2    qq    i2     1163     2 # This should be 1
18 p2    r1    i1     1263     1 # This should be 2
19 p2    r2    i1     1263     1 # This should be 2
20 p2    r3    i1     1295     1 # This should be 2
21 p2    r4    i1     1495     1 # This should be 2
22 p2    qq    i1     1495     1 # This should be 2
23 p2    r1    i3     1414     3 # This should be 3
24 p2    r2    i3     1414     3 # This should be 3
25 p2    r3    i3     1414     3 # This should be 3
26 p2    qq    i3     1484     3 # This should be 3
27 p3    r1    i3     1484     2 # This should be 1
28 p3    r2    i3     1182     2 # This should be 1
29 p3    r3    i3     1182     2 # This should be 1
30 p3    qq    i3     1282     2 # This should be 1
31 p3    r1    i1     1282     1 # This should be 2
32 p3    r2    i1     1282     1 # This should be 2
33 p3    r3    i1     1232     1 # This should be 2
34 p3    r4    i1     1232     1 # This should be 2
35 p3    qq    i1     1234     1 # This should be 2

MWE

data |>
  group_split(p) |>
  purrr::map_df(
    ~ .x |>
      group_by(i) |>
      mutate(
        trial_order = cur_group_id(),
      )
  )

CodePudding user response:

You could use match() unique():

library(dplyr)

data %>%
  group_by(p) %>%
  mutate(ID = match(i, unique(i))) %>%
  ungroup()

# A tibble: 35 × 5
   p     r     i        rt    ID
   <chr> <chr> <chr> <dbl> <int>
 1 p1    r1    i1     1234     1
 2 p1    r2    i1     1256     1
 3 p1    r3    i1     1256     1
 4 p1    r4    i1     1256     1
 5 p1    qq    i1     1356     1
 6 p1    r1    i2     1356     2
 7 p1    r2    i2     1356     2
 8 p1    r3    i2     1376     2
 9 p1    qq    i2     1374     2
10 p1    r1    i3     1374     3
11 p1    r2    i3     1174     3
12 p1    r3    i3     1174     3
13 p1    qq    i3     1173     3
14 p2    r1    i2     1163     1
15 p2    r2    i2     1163     1
16 p2    r3    i2     1163     1
17 p2    qq    i2     1163     1
18 p2    r1    i1     1263     2
19 p2    r2    i1     1263     2
20 p2    r3    i1     1295     2
21 p2    r4    i1     1495     2
22 p2    qq    i1     1495     2
23 p2    r1    i3     1414     3
24 p2    r2    i3     1414     3
25 p2    r3    i3     1414     3
26 p2    qq    i3     1484     3
27 p3    r1    i3     1484     1
28 p3    r2    i3     1182     1
29 p3    r3    i3     1182     1
30 p3    qq    i3     1282     1
31 p3    r1    i1     1282     2
32 p3    r2    i1     1282     2
33 p3    r3    i1     1232     2
34 p3    r4    i1     1232     2
35 p3    qq    i1     1234     2
  • Related