mutate_impl(.data,点)评估错误:找不到对象 [英] mutate_impl(.data, dots) Evaluation error: object not found

查看:223
本文介绍了mutate_impl(.data,点)评估错误:找不到对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些有效的代码。我不得不更新R(并再次安装所有软件包),当我尝试运行代码时再次碰壁。这是一个玩具示例:

I had some working code. I had to update R (and install all packages again) and when I try to run the code again hit a wall. Here's a toy example:

工作代码

# get cyl column
mtcars %>% dplyr::select(cyl)
# add 1 to all numeric
mtcars %>% dplyr::mutate_if(is.numeric, ~.+1)

Wall

但是,当我尝试将所有数字列除以 cyl 列时,我不能。

mtcars %>% mutate_if(is.numeric, ~./cyl)
Error in mutate_impl(.data, dots) : 
  Evaluation error: object 'cyl' not found.

顺便说一句...可行

mtcars %>% mutate_if(is.numeric, ~./mtcars$cyl)

由于某些原因, mutate_if 找不到该列(尽管 select 可以找到)。 purrr 包中的
map_if 具有相同的行为。

For some reason mutate_if is not finding the column (although select does). map_if from purrr package has the same behavior.

推荐答案

尝试添加 funs()

library(dplyr)

mtcars %>% 
  mutate_if(is.numeric, funs(./cyl))
#>         mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> 1  3.500000   1 160.0 110 3.90 2.620 16.46  0  1    4    4
#> 2  3.500000   1 160.0 110 3.90 2.875 17.02  0  1    4    4
#> 3  5.700000   1 108.0  93 3.85 2.320 18.61  1  1    4    1
#> 4  3.566667   1 258.0 110 3.08 3.215 19.44  1  0    3    1
#> 5  2.337500   1 360.0 175 3.15 3.440 17.02  0  0    3    2
#> 6  3.016667   1 225.0 105 2.76 3.460 20.22  1  0    3    1
#> 7  1.787500   1 360.0 245 3.21 3.570 15.84  0  0    3    4
#> 8  6.100000   1 146.7  62 3.69 3.190 20.00  1  0    4    2
#> 9  5.700000   1 140.8  95 3.92 3.150 22.90  1  0    4    2
#> 10 3.200000   1 167.6 123 3.92 3.440 18.30  1  0    4    4


mtcars %>% 
  mutate_if(is.numeric, funs(div = ./cyl))
#>     mpg cyl  disp  hp drat    wt  qsec vs am gear carb  mpg_div cyl_div
#> 1  21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4 3.500000       1
#> 2  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4 3.500000       1
#> 3  22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1 5.700000       1
#> 4  21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1 3.566667       1
#> 5  18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2 2.337500       1
#> 6  18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1 3.016667       1
#> 7  14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4 1.787500       1
#> 8  24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2 6.100000       1
#> 9  22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2 5.700000       1
#> 10 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4 3.200000       1
#>    disp_div   hp_div  drat_div    wt_div qsec_div    vs_div    am_div
#> 1  26.66667 18.33333 0.6500000 0.4366667 2.743333 0.0000000 0.1666667
#> 2  26.66667 18.33333 0.6500000 0.4791667 2.836667 0.0000000 0.1666667
#> 3  27.00000 23.25000 0.9625000 0.5800000 4.652500 0.2500000 0.2500000
#> 4  43.00000 18.33333 0.5133333 0.5358333 3.240000 0.1666667 0.0000000
#> 5  45.00000 21.87500 0.3937500 0.4300000 2.127500 0.0000000 0.0000000
#> 6  37.50000 17.50000 0.4600000 0.5766667 3.370000 0.1666667 0.0000000
#> 7  45.00000 30.62500 0.4012500 0.4462500 1.980000 0.0000000 0.0000000
#> 8  36.67500 15.50000 0.9225000 0.7975000 5.000000 0.2500000 0.0000000
#> 9  35.20000 23.75000 0.9800000 0.7875000 5.725000 0.2500000 0.0000000
#> 10 27.93333 20.50000 0.6533333 0.5733333 3.050000 0.1666667 0.0000000

由< a href = https://reprex.tidyverse.org rel = noreferrer> reprex包(v0.2.1.9000)

Created on 2018-10-09 by the reprex package (v0.2.1.9000)

这篇关于mutate_impl(.data,点)评估错误:找不到对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆