tidyselect::where() 不一致:where() 在哪里? 数据

如何解决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_withends_withcontains,或在 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?