消除dplyr冗余功能行

时间:2019-08-12 15:45:34

标签: r dplyr

library(dplyr)

# Example 1 -----------------------------------------------------
mtcars %>% 
  mutate(dubcyl = cyl * 2, dubcyl.avg = mean(.$dubcyl)) %>% 
  as_tibble()
#> Warning message:
#> In mean.default(.$dubcyl) :
#>   argument is not numeric or logical: returning NA

# Example 2 -----------------------------------------------------
mtcars %>% 
  mutate(dubcyl = cyl * 2) %>% 
  mutate(dubcyl.avg = mean(.$dubcyl)) %>% 
  as_tibble()

是否有一种方法可以创建我想要的dubcyl列,如示例1 所示,而不必两次突变,如示例2 所示?

1 个答案:

答案 0 :(得分:0)

我们在这里不需要.$

library(tibble)
librrary(dplyr)
mtcars %>% 
   mutate(dubcyl = cyl * 2, dubcyl.avg = mean(dubcyl)) %>% 
   as_tibble()