摘要:你可以重命名(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)
发布于 2021-02-22 22:32:13
与rename_at
相比,rename_with
中的参数发生了切换。关于代码中指定的列名和显示的数据有一点不清楚,特别是在两个参数中都有str_replace
的情况下。将以'Var_2‘开头的列名替换为'Issue: Time_2’的典型用法是
library(dplyr)
data <- data %>%
rename_with(~ str_replace(., 'Var_2', 'Issue: Time'),
starts_with('Var_2'))
-output
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
或者如果我们想要改变相应的替换,模式,使用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(., 'Var_2', 'Issue: Time'), 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的代码中,可以用summarise/across
替换select/map
df %>%
summarise(across(where(is.numeric), sd))
数据
data <- as_tibble(test)
https://stackoverflow.com/questions/66324365
复制相似问题