0

I am analyzing a dataset including a participants' ID number, which expresses each participant's unique ID in a certain group (e.g., worker id, registered id, or whatever).

Because I would like to upload the dataset online, I need to provide different IDs for each participant. The dataset was shaped into a long data to analyze with a generalized linear mixed-effects model (GLMM) as follows:

enter image description here

I would like to replace the ids, starting from 1. The following is one potential resulting dataset I would like to obtain: enter image description here

I have been trying to use pivot_longer and pivot_wide for hours, but could not figure out how to do this.

I would appreciate it if you could demonstrate how I can replace the id column with new ids.

Here are some replicable dataset:

  structure(list(id = c(1001L, 1001L, 1001L, 1001L, 1001L, 1001L, 
1001L, 1001L, 1001L, 1002L, 1002L), condition_a = c("a", "a", 
"a", "b", "b", "b", "c", "c", "c", "a", "a"), condition_b = c("aa", 
"aa", "aa", "aa", "aa", "aa", "aa", "aa", "aa", "bb", "bb"), 
    condition_c = c("abc", "abc", "abc", "abc", "abc", "abc", 
    "abc", "abc", "abc", "abc", "abc"), condition_d = c(10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 15L, 15L), items = c("a", 
    "b", "c", "d", "e", "f", "g", "h", "i", "a", "b"), response = c(0L, 
    1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L)), class = "data.frame", row.names = c(NA, 
-11L))
user8460166
  • 73
  • 1
  • 6
  • 24

2 Answers2

1

Not sure I fully understand the extent of the transformation you want but if it's just creating new indices starting at one this will do the trick.

library(dplyr)
df3 %>%
  group_by(id) %>%
  mutate(id = cur_group_id())
Quixotic22
  • 2,894
  • 1
  • 6
  • 14
1

You use group_indices along with mutate:

library(tidyverse)
df %>% 
  mutate(id = group_indices(df, .dots = c('id')))

#   id condition_a condition_b condition_c condition_d items response
#1   1           a          aa         abc          10     a        0
#2   1           a          aa         abc          10     b        1
#3   1           a          aa         abc          10     c        0
#4   1           b          aa         abc          10     d        1
#5   1           b          aa         abc          10     e        0
#6   1           b          aa         abc          10     f        1
#7   1           c          aa         abc          10     g        0
#8   1           c          aa         abc          10     h        1
#9   1           c          aa         abc          10     i        0
#10  2           a          bb         abc          15     a        1
#11  2           a          bb         abc          15     b        0

patL
  • 2,259
  • 1
  • 17
  • 38
  • Thank you for your comment, @patL. Sorry, my sample dataset was not so clear. The `id` was actually random in the real dataset, so I cannot just delete the first 3 characters in `id`. – user8460166 Oct 08 '21 at 10:07
  • Thank you for providing another way to deal with this! :) – user8460166 Oct 08 '21 at 13:59