I wonder how to pass specific arguments to only specific function in mutate_if
, mutate_at
, mutate_all
, etc functions that often take a function(x, fun, args)
structure. collap
function from the collapse
package works quite the same. For instance, I wrote a custom function that looks for specific values and returns a factor for an aggregation using collap
function. In the code below, I want to pass look
argument to only alo
function, but it's being passed to fsum
as well. However, this code is still working (see the warning), but if other functions had the same argument, it would cause problems. Any help is appreciated.
library(collapse)
#> collapse 2.0.7, see ?`collapse-package` or ?`collapse-documentation`
#>
#> Attaching package: 'collapse'
#> The following object is masked from 'package:stats':
#>
#> D
alo <- function(var, look){
x <- any(var %in% look)
y <- data.table::fifelse(x, yes = "Yes", no = "No")
z <- base::factor(y, levels = c("No", "Yes"))
return(z)
}
df <- data.frame(id = c("A", "A", "B", "B", "B"),
x1 = c(8, 2, 7, 9, 8),
x2 = c("No", "No", "Yes", "No", "No"))
df
#> id x1 x2
#> 1 A 8 No
#> 2 A 2 No
#> 3 B 7 Yes
#> 4 B 9 No
#> 5 B 8 No
df |> collap(~id, FUN = fsum, catFUN = alo, look = "Yes")
#> Warning in unused_arg_action(match.call(), ...): Unused argument (look = "Yes")
#> passed to fsum.data.frame
#> id x1 x2
#> 1 A 10 No
#> 2 B 24 Yes
Created on 2024-01-12 with reprex v2.1.0