I have a dataframe like this:
test <- data.frame(
Participant = c("Participant 1", "Participant 1", "Participant 2", "Participant 3"),
PID = c("E1234324", "E20983402", "E00990348", "E34788348")
)
I want to extract the number from each Participant
value, such that this is the desired dataframe output:
test1 <- data.frame(
Participant = c(1,1,2,3),
PID = c("E1234324", "E20983402", "E00990348", "E34788348")
)
This is the code I have so far:
test1 <- test %>%
mutate(Participant = str_sub(test$Participant, start = -1)) %>%
mutate(Participant = as.numeric(Participant))
But I get this error:
Error in `mutate()`:
ℹ In argument: `Participant = str_sub(test$Participant, start = -1)`.
Caused by error:
! `Participant` must be size 0 or 1, not 6.
I’m not sure how to modify this — does anyone have any leads?
Thank you!