I have a dataframe with several columns. One of them is the column participant, where different participant codes are listed. These are all either in the 100 range, the 200 range or the 500 range.
For example: 101, 203, 209, 504, 103, 512 and so on.
I want to create an extra column in the dataframe called group with 3 possible values: 100, 200 and 500. Thus, depending on the number a participant code starts with, it will be assigned one of these 3 labels.
I have tried using a combination of startsWith() and ifelse statements, but I can't make it work.
data$group = ifelse(startsWith(as.character(data$participant), "1"), "100",
((ifelse(startsWith(as.character(data$participant), "2"), "200",
(ifelse(startsWith(as.character(data$participant), "5"), "500")), NULL)))