I recoded specific columns in my dataframe using this code:
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)))
which works perfectly, but I don't really understand the second argument in the mutate_at function. Why do I need to wrap the recode() function inside funs(), and why do I use a period argument inside recode? My understanding is that mutate_at takes a vars() argument and a function to apply to all the columns specified inside vars. So isn't the funs() redundant?
As of version 0.8.0 of dplyr the use of funs()
is soft depricated.
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
This means that by using version 0.8.0 or newer you don't have to use funs()
. You simply use it as before just without 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
likewise, you can create a function and pass that in too.
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
Created on 2019-12-08 by the reprex package (v0.3.0)