-
-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
new feature: data_apply() #351
Comments
Since it has a |
What would be the use cases? Don't we have a lot of data functions that cover most situations? like |
Else, we still could do: data[] <- data |>
data_select() |>
lapply(<fun>) |
yes but that looks quite hacky
mostly custom functions. basically same as sapply, but with a dataframe output + some selection helpers |
Unless you mean that this would be the code of |
Normal pipe workflow? 🤷 |
Doesn't work ( library(datawizard)
foo <- mtcars[, 1:5]
foo[] <- foo |>
data_select(exclude = "mpg") |>
lapply(as.factor)
lapply(foo, class)
#> $mpg
#> [1] "factor"
#>
#> $cyl
#> [1] "factor"
#>
#> $disp
#> [1] "factor"
#>
#> $hp
#> [1] "factor"
#>
#> $drat
#> [1] "factor" Can be done quite easily though: library(datawizard)
foo <- mtcars[, 1:5]
to_convert <- foo |>
data_find(exclude = "mpg")
foo[to_convert] <- lapply(foo[to_convert], as.factor)
lapply(foo, class)
#> $mpg
#> [1] "numeric"
#>
#> $cyl
#> [1] "factor"
#>
#> $disp
#> [1] "factor"
#>
#> $hp
#> [1] "factor"
#>
#> $drat
#> [1] "factor" |
data_apply <- function(data, fun, ...) {
to_convert <- data_find(data, ...)
data[to_convert] <- as.data.frame(lapply(data[to_convert], fun))
data
}
It's a small addition to the codebase but quite convenient imo |
Should this be an internal function in the package where you want to use it? Personally I'm a bit reluctant because it's getting quite close to That said, @easystats/core-team what do you think? |
I was thinking of an external one as a combination of base's apply + selection |
I still rely pretty heavily on |
I'm not really understanding the use case here? There's pretty well-known and reasonably convenient base syntax like Daniel showed for developers, and map(), mutate(), and summarize() exist for end users |
As far as base goes, I don't see why one would use this (or sapply() in general) over apply(., 2) ? |
Would be nice to have a small wrapper for
sapply()
to run on dataframes to do things like:The text was updated successfully, but these errors were encountered: