我需要从数据框中删除“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”?谢谢你的帮助。