r - mutate_if - 警告信息
问题描述
大家好,
library(dplyr)
library(tibble)
mtcars %>%
rownames_to_column("modelle") %>%
mutate_if(~is.numeric(.x) & mean(.x) > 50, ~(.x / 1000))
Warning message:
In mean.default(.x) : argument is not numeric or logical: returning NA
这个错误似乎是因为字符向量。它有效,但它仍然非常丑陋。我有没有做错什么,在这种情况下可以做得更好吗?
谢谢!
解决方案
R 不会短路 vectorized ,&
因此它在所有列上都运行。 由于您的第一列 ( ) 显然是,因此它失败了。is.numeric
mean
modelle
character
但是,您实际上不需要对其进行矢量化。如果您从 vectorized 更改&
为 binary &&
,R 会将其短路并获得您想要的行为。
mtcars %>%
rownames_to_column("modelle") %>%
mutate_if(~is.numeric(.x) && mean(.x) > 50, ~(.x / 1000)) %>%
head()
# modelle mpg cyl disp hp drat wt qsec vs am gear carb
# 1 Mazda RX4 21.0 6 0.160 0.110 3.90 2.620 16.46 0 1 4 4
# 2 Mazda RX4 Wag 21.0 6 0.160 0.110 3.90 2.875 17.02 0 1 4 4
# 3 Datsun 710 22.8 4 0.108 0.093 3.85 2.320 18.61 1 1 4 1
# 4 Hornet 4 Drive 21.4 6 0.258 0.110 3.08 3.215 19.44 1 0 3 1
# 5 Hornet Sportabout 18.7 8 0.360 0.175 3.15 3.440 17.02 0 0 3 2
# 6 Valiant 18.1 6 0.225 0.105 2.76 3.460 20.22 1 0 3 1
进一步证明&
不是短路。
mymean <- function(x, ...) {
if (is.character(x)) {
message("character?")
return(Inf) # this is certainly not the right thing to do in general ...
} else mean(x, ...)
}
mtcars %>%
rownames_to_column("modelle") %>%
mutate_if(~is.numeric(.x) & mymean(.x) > 50, ~(.x / 1000)) %>%
head()
# character?
# modelle mpg cyl disp hp drat wt qsec vs am gear carb
# 1 Mazda RX4 21.0 6 0.160 0.110 3.90 2.620 16.46 0 1 4 4
# 2 Mazda RX4 Wag 21.0 6 0.160 0.110 3.90 2.875 17.02 0 1 4 4
# 3 Datsun 710 22.8 4 0.108 0.093 3.85 2.320 18.61 1 1 4 1
# 4 Hornet 4 Drive 21.4 6 0.258 0.110 3.08 3.215 19.44 1 0 3 1
# 5 Hornet Sportabout 18.7 8 0.360 0.175 3.15 3.440 17.02 0 0 3 2
# 6 Valiant 18.1 6 0.225 0.105 2.76 3.460 20.22 1 0 3 1
如果发生短路,则永远mymean
不会收到消息。(我认为这mymean
不是一个可行的替代品,原因有两个:(1)使用Inf
只是为了确保调用工作之外mean
的条件,但如果发生错误/警告并且numeric
预期会出现 a,那么通常应该返回NA
or NaN
,而不是数字......即使您可能不考虑Inf
真正可用的数字。(2)它解决的是症状,而不是问题。问题是向量化逻辑表达式中没有短路.)
推荐阅读
- python - 对包装自定义 Airflow 插件的函数进行单元测试
- php - 如何在 ci4 路由中传递任意数量的参数?
- php - 在 Telegram 数组中使用 While 循环
- python - /login login() 的 TypeError 缺少 1 个必需的位置参数:“用户”
- javascript - 当前未启用对实验性语法“jsx”的支持
- ios - Xcode:如何保存可编辑的对象数组并使用它来填充表格视图?
- python - 如何在读取包含大量行的大型 excel 时使代码运行得更快
- python - python如何在没有我定义的情况下猜测正在定义的函数中的参数?
- deep-learning - 如何确定 DQN 模型中使用正奖励还是负奖励?
- python - Python:有没有办法过滤 c++ 文件?