I'm trying to replicate some Stata code in r. In my df, there is a variable "time" and one "exposure" (both numeric, so with values like 1,2,3 etc.
This is what the original Stata code looks like:
replace time = (time - 31) if exposure == 0
And this is what I tried:
recoded_df <- recoded_df %>% mutate(time = case_when(exposure == 0 ~ (time - 31))
I get the error that there is an "unexpected symbol" in the code. But I have no idea what I am doing wrong.
dput(head(data))? It's essential if you are to have any chances of getting relevant solutions.)at the end. You probably also want to set a default of "no changes",mutate(time = case_when(exposure == 0 ~ (time - 31), TRUE ~ time))mutate(time = case_when(...) time2 = foo(.)), where there needs to be a comma (just a guess, though).case_whendoes default toNAwhich might be what the OP is intending. (idk)