总结:你可以做rename(A=1, B=2),你能用rename_with()做同样的事情吗?我的 ~str_replace(... paste0()) 有效,我不需要改变它。但它一次只适用于一个变量。Tidyselect 建议包装 where(~str_replace...) 但随后抱怨它找不到它,即使我可以让 where() 在其他情况下工作。
我想为多个变量实现 rename_with,但我得到一个错误Error: Formula shorthand must be wrapped in
where()`。
# Bad
data %>% select(~str_replace(., "Var_2_", paste0("Issue: Time")))
# Good
data %>% select(where(~str_replace(., "Var_2_", paste0("Issue: time"))))
示例原文:
test%>% rename_with( ~str_replace(., "Var_2_", paste0("Issue: Time")), ~str_replace(., "Var_3_", paste0("Issue: Time")))
当我跑步时
test%>% rename_with(where( ~str_replace(., "Var_2_", paste0("Issue: Time")), ~str_replace(., "Var_3_", paste0("Issue: Time"))))
和
test%>% rename_with( where(~str_replace(., "Var_2_", paste0("Issue: Time"))), where(~str_replace(., "Var_3_", paste0("Issue: Time"))))
我明白
Error in where(~str_replace(., "Var_1_", paste0("Gov't surveillance: video wave")), : could not find function "where"
了,我找不到它通过tidyselect::
但我可以
test%>% select(where(is.numeric)) %>% map(sd, na.rm = TRUE)
毫无问题地运行,所以它确实存在。我究竟做错了什么?
示例数据:
x <- c("_1_1",
"_1_2",
"_1_3",
"_2_1",
"_2_2",
"_2_3",
"_3_1",
"_3_2",
"_3_3",
"_4_3")
paste0("Var",x)
test <- t(as_tibble(rnorm(10, 5.5, .35)))
colnames(test) <- paste0("Var",x)