0

我很可能遗漏了一些非常简单的东西,但我无法弄清楚为什么这个错误会持续存在于我的 mutate_at 函数中。

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

我正在尝试使用 mutate_at 在变量名称末尾添加后缀(“_cat”)的新列,但是在尝试选择包含“diff_”的变量时出现以下错误,我无法弄清楚为什么......: '(

Error: `.vars` must be a character/numeric vector or a `vars()` object, not a `data.frame` object

编辑:

当我分段运行 R markdown 时,测试代码在我重新启动 R 后工作。但每当我尝试编织整个 markdown 工作簿时,错误消息仍然存在。这意味着我不能只重新启动 RStudio 来修复此错误。

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

我已经下载了最新版本的 R 和 RStudio,但仍然没有运气 :(。我想知道是否有人可以解释为什么会出现此错误消息?是 tidyselect 函数有问题吗?

谢谢!

编辑2:

作为参考,本节的完整代码为:

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

vars(contains("diff_")当我编织 Rmarkdown 时,问题似乎出在我代码的最后一行。目前,我有一个非常粗略的解决方法,我已经列出了变量,而不是这样:

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

我能够使用这种粗略的解决方法来编织输出,但是知道为什么我的第一次尝试失败以供将来参考会非常方便。

4

0 回答 0