小编roc*_*und的帖子

如何在不使所有列都变为字符的情况下在整个数据帧上应用正则表达式

我需要从数据框中删除“Z”:

df <- data.frame(Mineral = c("Zfeldspar", "Zgranite", "ZSilica"),
                Confidence = c("ZLow", "High", "Med"),
                Coverage = c("sub", "sub", "super"),
                Aspect = c("ZPos", "ZUnd", "Neg"),
                Pile1 = c(70, 88, 95),
                Pile2 = c(62,41,81))
Run Code Online (Sandbox Code Playgroud)

我使用了 tidyverse:

library(tidyverse)

df <- mutate_all(df, funs(str_replace_all(., "Z", ""))) %>%
      mutate(PileAvg = mean(Pile1 + Pile2))
Run Code Online (Sandbox Code Playgroud)

但我得到错误

Error in mutate_impl(.data, dots) : 
  Evaluation error: non-numeric argument to binary operator.
Run Code Online (Sandbox Code Playgroud)

我做了调查,这是因为 Pile 列现在是字符,而不是数字。如何在不更改所有内容的情况下使用正则表达式删除“Z”?谢谢你的帮助。

regex r dplyr

5
推荐指数
1
解决办法
166
查看次数

标签 统计

dplyr ×1

r ×1

regex ×1