如何解决tidyselect::where() 不一致:where() 在哪里? 数据
总结:你可以做 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(.,paste0("Issue: time"))))
示例原件:
test%>% rename_with( ~str_replace(.,paste0("Issue: Time")),~str_replace(.,"Var_3_",paste0("Issue: Time")))
当我跑步时
test%>% rename_with(where( ~str_replace(.,paste0("Issue: Time"))))
和
test%>% rename_with( where(~str_replace(.,paste0("Issue: Time"))),where(~str_replace(.,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)
解决方法
与 rename_with
相比,rename_at
中的参数发生了变化。代码中指定的列名有点不清楚,特别是两个参数中的 str_replace
显示的数据。将以“Var_2”开头的列名替换为“Issue: Time_2”的典型用途是
library(dplyr)
data <- data %>%
rename_with(~ str_replace(.,'Var_2','Issue: Time'),starts_with('Var_2'))
-输出
data
# A tibble: 1 x 10
# Var_1_1 Var_1_2 Var_1_3 `Issue: Time_1` `Issue: Time_2` `Issue: Time_3` Var_3_1 Var_3_2 Var_3_3 Var_4_3
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 5.68 5.18 5.34 5.38 5.47 5.82 5.93 5.35 5.20 5.62
如果我们需要改变多个列模式,使用matches
data %>%
rename_with(~ str_replace(.,'(Var_2|Var_3)','\\1_Issue: Time'),matches('Var_2|Var_3'))
# A tibble: 1 x 10
# Var_1_1 Var_1_2 Var_1_3 `Var_2_Issue: Tim… `Var_2_Issue: Tim… `Var_2_Issue: Tim… `Var_3_Issue: Ti… `Var_3_Issue: Ti… `Var_3_Issue: Ti… Var_4_3
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 5.68 5.18 5.34 5.38 5.47 5.82 5.93 5.35 5.20 5.62
或者如果我们想改变相应的replacement,pattern,使用str_replace_all
data1 <- data %>%
set_names(str_replace_all(names(.),c("Var_1","Var_2"),c("Issue 1 wave","Issue 2 Wave")))
比较输出
data1
# A tibble: 1 x 10
`Issue 1 wave_1` Var_1_2 `Issue 1 wave_3` `Trust Wave_1` Var_2_2 `Issue 2 Wave_3` Var_3_1 Var_3_2 Var_3_3 Var_4_3
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 5.68 5.18 5.34 5.38 5.47 5.82 5.93 5.35 5.20 5.62
原始数据
data
# A tibble: 1 x 10
Var_1_1 Var_1_2 Var_1_3 Var_2_1 Var_2_2 Var_2_3 Var_3_1 Var_3_2 Var_3_3 Var_4_3
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 5.68 5.18 5.34 5.38 5.47 5.82 5.93 5.35 5.20 5.62
where
通常用于检查列值,即假设我们要选择数字类型的列,请使用 select(where(is.numeric))
而不是列名。有 select_helpers 可以根据子字符串查找列名,即 starts_with
、ends_with
、contains
,或在 matches
中传递正则表达式模式。 where
的一个用例是
data %>%
rename_with(~ str_replace(.,where(~ all(. > 5.5)))
# A tibble: 1 x 10
# Var_1_1 Var_1_2 Var_1_3 Var_2_1 Var_2_2 `Issue: Time_3` Var_3_1 Var_3_2 Var_3_3 Var_4_3
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 5.68 5.18 5.34 5.38 5.47 5.82 5.93 5.35 5.20 5.62
在 OP 的代码中,select/map
可以替换为 summarise/across
df %>%
summarise(across(where(is.numeric),sd))
数据
data <- as_tibble(test)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。