Reputation: 6615
Looking for some advice on how to use dplyr's mutate_if statement to check to see if I need to convert a column to a factor variable.
This is function illustrates what I'm trying to do. The problem is, what is the correct syntax when I want to pass in something for the "max_value" parameter in my function?
Doesn't work - I try to change parameter within function.
funct_change <- function(x, max_value ){
max(x, na.rm = TRUE) >max_value
}
mtcars %>% mutate_if( funct_change(max_value=30), as.character) %>% glimpse()
Works - I hardcode the parameter
funct_change <- function(x, max_value=30 ){
max(x, na.rm = TRUE) >max_value
}
mtcars %>% mutate_if( funct_change, as.character) %>% glimpse()
Upvotes: 5
Views: 1013
Reputation: 15072
If you're supplying anything other than a bare function name to .predicate
in mutate_if
(and also other places that .funs
crops up in dplyr
), you need to do one of the following:
~
to do basically the same thing....
lets you add extra arguments to .funs
, so you could supply max_value = 30
as an argument to mutate
. For mutate_if
, that only works for the function to apply, not the .predicate
.funct_change <- function(x, max_value){
max(x, na.rm = TRUE) > max_value
}
library(dplyr)
mtcars %>% mutate_if(function(x) funct_change(x, 30), as.character) %>% glimpse()
#> Observations: 32
#> Variables: 11
#> $ mpg <chr> "21", "21", "22.8", "21.4", "18.7", "18.1", "14.3", "24.4...
#> $ cyl <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, ...
#> $ disp <chr> "160", "160", "108", "258", "360", "225", "360", "146.7",...
#> $ hp <chr> "110", "110", "93", "110", "175", "105", "245", "62", "95...
#> $ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.9...
#> $ wt <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3...
#> $ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 2...
#> $ vs <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, ...
#> $ am <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, ...
#> $ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, ...
#> $ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, ...
mtcars %>% mutate_if(~ funct_change(., 30), as.character) %>% glimpse()
#> Observations: 32
#> Variables: 11
#> $ mpg <chr> "21", "21", "22.8", "21.4", "18.7", "18.1", "14.3", "24.4...
#> $ cyl <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, ...
#> $ disp <chr> "160", "160", "108", "258", "360", "225", "360", "146.7",...
#> $ hp <chr> "110", "110", "93", "110", "175", "105", "245", "62", "95...
#> $ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.9...
#> $ wt <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3...
#> $ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 2...
#> $ vs <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, ...
#> $ am <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, ...
#> $ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, ...
#> $ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, ...
Created on 2018-04-10 by the reprex package (v0.2.0).
Upvotes: 4