首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >tidyselect:: where ()不一致:where is where()?

tidyselect:: where ()不一致:where is where()?
EN

Stack Overflow用户
提问于 2021-02-23 06:29:22
回答 1查看 126关注 0票数 0

摘要:你可以重命名(A=1,B=2),你能用rename_with()做同样的事情吗?我的~str_replace(...Paste0()可以工作,我不需要更改它。但它一次只对一个变量有效。Tidyselect建议换行where(~str_replace...)但随后抱怨说,即使我可以在其他实例中使用where(),它也找不到它。

我想为多个变量实现rename_with,但是我得到了一个错误Error: Formula shorthand must be wrapped in where()`。

代码语言:javascript
运行
复制
# 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),所以它确实存在。我做错了什么?

示例数据:

代码语言:javascript
运行
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2021-02-23 06:32:13

rename_at相比,rename_with中的参数发生了切换。关于代码中指定的列名和显示的数据有一点不清楚,特别是在两个参数中都有str_replace的情况下。将以'Var_2‘开头的列名替换为'Issue: Time_2’的典型用法是

代码语言:javascript
运行
复制
library(dplyr)
data <- data %>% 
    rename_with(~ str_replace(., 'Var_2', 'Issue: Time'), 
       starts_with('Var_2'))

-output

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
data1 <- data %>%
   set_names(str_replace_all(names(.), c("Var_1", "Var_2"), c("Issue 1 wave", "Issue 2 Wave")))

比较输出

代码语言:javascript
运行
复制
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

使用原始数据

代码语言:javascript
运行
复制
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_withends_withcontains )查找列名,或者在matches中传递正则表达式模式。where的一个用例是

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
df %>%
    summarise(across(where(is.numeric), sd))

数据

代码语言:javascript
运行
复制
data <- as_tibble(test)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66324365

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档