Mutate_at 函数与 Recode 结合使用时如何工作?

How does the Mutate_at function work when combined with Recode?

我使用以下代码重新编码了数据框中的特定列:

ptsd_copy %>%
mutate_at(vars(AAQ_1, AAQ_4, AAQ_5, AAQ_6), funs(recode(., 
                                                   'never true' = 7,
                                                    'often untrue' = 6,
                                                    'sometimes untrue' = 5,
                                                    'undecided' = 4,
                                                    'sometimes true' = 3,
                                                    'often true' = 2,
                                                    'always true' = 1))) %>%
mutate_at(vars(AAQ_2, AAQ_3, AAQ_7, AAQ_8, AAQ_9), funs(recode (.,
                                                           'never true' = 1,
                                                            'often untrue' = 2,
                                                            'sometimes untrue' = 3,
                                                            'undecided' = 4,
                                                            'sometimes true' = 5,
                                                            'often true' = 6,
                                                            'always true' = 7)))

效果很好,但我不太理解 mutate_at 函数中的第二个参数。为什么我需要将 recode() 函数包装在 funs() 中,为什么我在 recode 中使用句点参数?我的理解是 mutate_at 接受一个 vars() 参数和一个函数来应用于 vars 中指定的所有列。那么 funs() 不是多余的吗?

dplyr 版本 0.8.0 开始,funs() 的使用被软描述。

library(dplyr)
iris %>%
  mutate_at(vars(Species), funs(recode(., 
                                       "setosa" = 1,
                                       "versicolor" = 2,
                                       "virginica" = 3))) %>%
  head()
#> Warning: funs() is soft deprecated as of dplyr 0.8.0
#> Please use a list of either functions or lambdas: 
#> 
#>   # Simple named list: 
#>   list(mean = mean, median = median)
#> 
#>   # Auto named with `tibble::lst()`: 
#>   tibble::lst(mean, median)
#> 
#>   # Using lambdas
#>   list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
#> This warning is displayed once per session.
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1          5.1         3.5          1.4         0.2       1
#> 2          4.9         3.0          1.4         0.2       1
#> 3          4.7         3.2          1.3         0.2       1
#> 4          4.6         3.1          1.5         0.2       1
#> 5          5.0         3.6          1.4         0.2       1
#> 6          5.4         3.9          1.7         0.4       1

这意味着通过使用 0.8.0 或更新版本,您不必使用 funs()。您只需像以前一样使用它,只是没有 funs()

iris %>%
  mutate_at(vars(Species), ~ recode(., 
                                    "setosa" = 1,
                                    "versicolor" = 2,
                                    "virginica" = 3)) %>%
  head()
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1          5.1         3.5          1.4         0.2       1
#> 2          4.9         3.0          1.4         0.2       1
#> 3          4.7         3.2          1.3         0.2       1
#> 4          4.6         3.1          1.5         0.2       1
#> 5          5.0         3.6          1.4         0.2       1
#> 6          5.4         3.9          1.7         0.4       1

同样,您也可以创建一个函数并将其传入。

recode_fun <- function(x) {
  recode(x, 
         "setosa" = 1,
         "versicolor" = 2,
         "virginica" = 3)
}

iris %>%
  mutate_at(vars(Species), recode_fun) %>%
  head()
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1          5.1         3.5          1.4         0.2       1
#> 2          4.9         3.0          1.4         0.2       1
#> 3          4.7         3.2          1.3         0.2       1
#> 4          4.6         3.1          1.5         0.2       1
#> 5          5.0         3.6          1.4         0.2       1
#> 6          5.4         3.9          1.7         0.4       1

reprex package (v0.3.0)

于 2019-12-08 创建