I have a large dataset (called 'cud1') to which I want to add a new column categorising multiple primary health complaints into more simple health categories ('q2.2_healthCat'). That is, primary health complaints 1, 2, 4 or 6 will be categorised as 'mental health' (category 1), responses 3, 5, 7 or 8 = pain (category 2), and all other responses (9, 10, 11, 12) are categorised as other (category 3). Here's a basic data frame to give you an idea:
Participant_ID <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Primary_health_complaint <- c(3, 7, 12, 11, 3, 1, 9, 4, 6, 2)
cud1 <- data.frame(Participant_ID, Primary_health_complaint)
Then I would like a new column saying:
q2.2_healthCat <- c(2, 2, 3, 3, 2, 1, 3, 1, 1, 1)
Here's my attempt (using case_when this time):
cud1 <- cud1 %>% mutate(q2.2_healthCat = case_when(
primary_health_complaint = c(1,2,4,6), '1',
primary_health_complaint = c(3,5,7,8), '2',
primary_health_complaint = c(9,10,11,12), '3'))
Hope someone can help! Please be kind, as I'm new to R. I've had a look at many other posts and can't figure out what I'm doing wrong.
Edit: Found the solution here case_when in mutate pipe using something along these lines:
require(data.table) ## 1.9.2+
setDT(df)
df[a %in% c(0,1,3,4) | c == 4, g := 3L]
df[a %in% c(2,5,7) | (a==1 & b==4), g := 2L]