It's highly likely that I'm missing something really simple, but I cannot work out why this error is persisting in my mutate_at function.
library(tidyverse)
test = data.frame(diff_1 = sample(0:10, size=20, replace=TRUE),
diff_2 = sample(0:10, size=20, replace=TRUE),
diff_3 = sample(0:10, size=20, replace=TRUE))
test2 = test %>% mutate_at(.vars = vars(contains("diff_")), .funs = list(cat = ~as.factor(ntile(., 5)))) # create quintiles of variables
I am trying to produce new columns with suffixes ("_cat") added at the end of variable names using mutate_at, but get the following error when trying to select variables that contain "diff_", which I cannot work out why...:'(
Error: `.vars` must be a character/numeric vector or a `vars()` object, not a `data.frame` object
EDIT:
When I run the R markdown in sections, the test code works after I have restarted R. But the error message persists whenever I try to knit the whole markdown workbook. This means that I can't just restart RStudio to fix this error.
Quitting from lines 1008-1040 (analysis-complete-case-FIoriginal.Rmd)
Error: `.vars` must be a character/numeric vector or a `vars()` object, not a `data.frame` object
I've downloaded the most recent versions of R and RStudio but still no luck :(. I wonder if anyone could shed light on why this error message might be occurring? Is it problem with the tidyselect function?
Thank you!
EDIT2:
For reference, the full code for this section is:
transposed_cont = mydata_cont %>%
pivot_longer(cols=all_of(myvarscont), names_to = "myvars", values_to = "value") %>%
pivot_wider(names_from = source, values_from = value) %>%
mutate(diff_w2w1 = wave2-wave1,
diff_w3w1 = wave3-wave1) %>%
pivot_wider(., id_cols=id, names_from=myvars, values_from=c("wave1", "wave2", "wave3", "diff_w2w1", "diff_w3w1")) %>% # allows you to spread multiple values
dplyr::select(id, wave1_fi.score, wave2_fi.score, wave1_fp.count, wave2_fp.count, starts_with("diff")) %>%
mutate_at(.vars = vars(contains("diff_"), .funs = list(cat = ~as.factor(ntile(., 5)))) ## THIS IS WHERE THE CODE FAILS
The problem seems to be in the last line of my code vars(contains("diff_")
when I knit the Rmarkdown. Currently, I have a very crude workaround where I've listed the variables instead as such:
tempvars = c(Cs(diff_w2w1_chronic_count, diff_w2w1_cvd_count, diff_w2w1_overnight_num, diff_w2w1_falls_count, diff_w2w1_mmse_errors,
diff_w2w1_MHcesd, diff_w2w1_COGimmediaterecall1, diff_w2w1_COGimmediaterecall2, diff_w2w1_COGdelayedrecall, diff_w2w1_bmi,
diff_w3w1_chronic_count, diff_w3w1_cvd_count, diff_w3w1_overnight_num, diff_w3w1_falls_count, diff_w3w1_FRtscore,
diff_w3w1_mmse_errors, diff_w3w1_moca_errors, diff_w3w1_MHcesd, diff_w3w1_COGimmediaterecall1, diff_w3w1_COGimmediaterecall2,
diff_w3w1_COGdelayedrecall, diff_w3w1_COGtrail1time, diff_w3w1_COGtrail2time, diff_w3w1_COGtraildeltatime, diff_w3w1_CRTmeancog,
diff_w3w1_CRTmeanmot, diff_w3w1_CRTmeantot, diff_w3w1_visualAcuityLeft, diff_w3w1_visualAcuityRight, diff_w3w1_bmi, diff_w3w1_FRwhr))
transposed_cont = mydata_cont %>%
pivot_longer(cols=all_of(myvarscont), names_to = "myvars", values_to = "value") %>%
pivot_wider(names_from = source, values_from = value) %>%
mutate(diff_w2w1 = wave2-wave1,
diff_w3w1 = wave3-wave1) %>%
pivot_wider(., id_cols=id, names_from=myvars, values_from=c("wave1", "wave2", "wave3", "diff_w2w1", "diff_w3w1")) %>% # allows you to spread multiple values
dplyr::select(id, wave1_fi.score, wave2_fi.score, wave1_fp.count, wave2_fp.count, starts_with("diff")) %>%
mutate_at(.vars = tempvars, .funs = list(cat = ~as.factor(ntile(., 5)))) # create quintiles of variables
I was able to knit the output with this crude workaround, but it would be very handy to know why my first attempt failed for future reference.