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 所示?
答案 0 :(得分:0)
我们在这里不需要.$
library(tibble)
librrary(dplyr)
mtcars %>%
mutate(dubcyl = cyl * 2, dubcyl.avg = mean(dubcyl)) %>%
as_tibble()