r - 使用 str_replace_all 替换数据框中的破折号
问题描述
我对这个基本问题感到困惑。我有一个带有破折号而不是零值的数据框,例如
example <- data.frame(Month = c("Jan", "Feb", "March"),
Units = c("100", "-", "300"),
stringsAsFactors = F)
现在我认为 str_replace_all 会起作用:
replace <- example %>%
str_replace_all("-", "0")
但我收到这个警告:
Warning message:
In stri_replace_first_regex(string, pattern, fix_replacement(replacement), :
argument is not an atomic vector; coercing
在将“单位”转换为数字之前,我需要先进行转换,例如
replace <- example %>%
str_replace_all("-", "0") %>%
mutate_at(2, as.numeric)
我究竟做错了什么?
解决方案
谢谢mutate_at
,因为我的真实数据对我有用,我有 21 个带有破折号的列,例如
replace <- real_data %>%
mutate_at(vars(2:22),list(~as.numeric(str_replace_all(.,'-','0')))))
推荐阅读
- python - 谁能告诉我为什么我一直遇到这个错误 - [TypeError: __init__() got an unexpected keyword argument 'strict']
- javascript - 将 javascript 字典传递给控制器上的 ASP.NET MVC 操作
- unity3d - 游戏对象在整个运动过程中都在振动
- java - Java 允许缩小引用转换
- c++ - 清理解决方案,现在在构建尝试时在同一行收到 4 个错误
- react-redux-firebase - 如何在 react-redux-firebase 中编写 firestore 批处理
- java - 如何使用 append 方法解决 TextView 无法添加新文本值
- c# - 在 C# linq 中结合多个具有可选参数的 where 条件?
- javascript - 相对于视口,Intersection Observer 是否从跨域 iframe 内部工作?
- php - (已解决)Laravel 全新安装 - 在没有 Brew 的情况下从 Mac 中删除 PHP 7.1.23