0

I have the following data frame:

enter image description here

I have multiple participants listed in subj_id (you can see only 2 here). I want to change all Pre items in the trial column to Pre1, Pre2, Pre3, Pre4.

How can I do this?

jamoreiras
  • 315
  • 1
  • 14
eegegg
  • 25
  • 3
  • 1
    what of `Test`? Do you also want to change it to `Test1 Test2` ...? Also note that one cannot replicate your work since you provided an image rather than plain text data – Onyambu Aug 27 '21 at 16:49
  • 1
    For context, images of code or data breaks screen readers (there are visually-impaired users who also need help) and breaks search-engine optimization; see https://meta.stackoverflow.com/a/285557 (and https://xkcd.com/2116/). Please read from https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info for some great suggestions on how to make the question more reproducible, starting with sample data (`dput(x)`) and code you've attempted. Thanks! – r2evans Aug 27 '21 at 16:58
  • @r2evans thanks so much! I wasn't aware of this. I'll certainly check out these websites. – eegegg Aug 27 '21 at 17:08

1 Answers1

1

base R

dat <- data.frame(
  subj_id = c(rep(1L, 7), rep(2L, 7)),
  trial = c(rep("Pre", 4), rep("Test", 3), rep("Pre", 4), rep("Test", 3)),
  judgment = 1:14
)
dat
#    subj_id trial judgment
# 1        1   Pre        1
# 2        1   Pre        2
# 3        1   Pre        3
# 4        1   Pre        4
# 5        1  Test        5
# 6        1  Test        6
# 7        1  Test        7
# 8        2   Pre        8
# 9        2   Pre        9
# 10       2   Pre       10
# 11       2   Pre       11
# 12       2  Test       12
# 13       2  Test       13
# 14       2  Test       14

ave(seq_len(nrow(dat)), dat[,c("subj_id", "trial")], FUN = seq_along)
#  [1] 1 2 3 4 1 2 3 1 2 3 4 1 2 3
dat$trial <- paste0(dat$trial, ave(seq_len(nrow(dat)), dat[,c("subj_id", "trial")], FUN = seq_along))

dat
#    subj_id trial judgment
# 1        1  Pre1        1
# 2        1  Pre2        2
# 3        1  Pre3        3
# 4        1  Pre4        4
# 5        1 Test1        5
# 6        1 Test2        6
# 7        1 Test3        7
# 8        2  Pre1        8
# 9        2  Pre2        9
# 10       2  Pre3       10
# 11       2  Pre4       11
# 12       2 Test1       12
# 13       2 Test2       13
# 14       2 Test3       14

If you only need "Pre" numbered, then alter that code above to be

dat$trial <- ifelse(dat$trial == "Pre", paste0(dat$trial, ave(seq_len(nrow(dat)), dat[,c("subj_id", "trial")], FUN = seq_along)), dat$trial)
dat
#    subj_id trial judgment
# 1        1  Pre1        1
# 2        1  Pre2        2
# 3        1  Pre3        3
# 4        1  Pre4        4
# 5        1  Test        5
# 6        1  Test        6
# 7        1  Test        7
# 8        2  Pre1        8
# 9        2  Pre2        9
# 10       2  Pre3       10
# 11       2  Pre4       11
# 12       2  Test       12
# 13       2  Test       13
# 14       2  Test       14

dplyr

library(dplyr)
dat %>%
  group_by(subj_id, trial) %>%
  mutate(trial = paste0(trial, row_number())) %>%
  ungroup()
# # A tibble: 14 x 3
#    subj_id trial judgment
#      <int> <chr>    <int>
#  1       1 Pre1         1
#  2       1 Pre2         2
#  3       1 Pre3         3
#  4       1 Pre4         4
#  5       1 Test1        5
#  6       1 Test2        6
#  7       1 Test3        7
#  8       2 Pre1         8
#  9       2 Pre2         9
# 10       2 Pre3        10
# 11       2 Pre4        11
# 12       2 Test1       12
# 13       2 Test2       13
# 14       2 Test3       14

Similarly, if only "Pre", then

dat %>%
  group_by(subj_id, trial) %>%
  mutate(trial = if_else(trial == "Pre", paste0(trial, row_number()), trial)) %>%
  ungroup()
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • Thank you so much! This is great. I just have two follow-up questions. (1) How can I only select only PRE trials (so not changing Test trials)? (2) is there a way to do this without depending on the subj_id? – eegegg Aug 27 '21 at 17:15
  • 1
    See my edit. I had assumed that that was not a factor since you never answered @Onyambu's comment. (This is one reason why the "reproducible question" links tend to suggest including expected output.) – r2evans Aug 27 '21 at 17:42
  • 1
    This is great help! Thanks! I'm now reading about the reproducible questions. Thank you so much for guiding me so much! – eegegg Aug 27 '21 at 18:01