Need to distinguish rows vs. cols #167
Closed
Labels
Comments
Or have |
Maybe |
I think I'd rather keep these as separate functions: these are special cases because data frames are so important in R. |
I guess this would work as a simple implementation. The naming code is a little weird though... map_dfc <- function(.x, .f, ..., .sep = "") {
.f <- as_function(.f, ...)
res <- map(.x, .f)
lens <- lengths(res)
ret <- vector("list", sum(lens))
i <- 1
for (j in seq_along(res)) {
for (k in seq_along(res[[j]])) {
ret[[i]] <- res[[j]][[k]]
i <- i + 1
}
}
nms <- map(res, names)
nms <- map(nms, ~ if (is.null(.)) "" else .)
names(ret) <- unlist(map2(names(nms), nms, paste, sep = .sep))
ret
}
map_dfc(mtcars, mean)
#> $mpg
#> [1] 20.09062
#>
#> $cyl
#> [1] 6.1875
#>
#> $disp
#> [1] 230.7219
#>
#> $hp
#> [1] 146.6875
#>
#> $drat
#> [1] 3.596563
#>
#> ...
map_dfc(mtcars, ~ tibble::data_frame(mean = mean(.), median = median(.)), .sep = "_")
#> $mpg_mean
#> [1] 20.09062
#>
#> $mpg_median
#> [1] 19.2
#>
#> $cyl_mean
#> [1] 6.1875
#>
#> $cyl_median
#> [1] 6
#>
#> $disp_mean
#> [1] 230.7219
#>
#> $disp_median
#> [1] 196.3
#> ... |
@jimhester why not use |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Maybe
map_dfc()
andmap_dfr()
? (Nice because then all types have a 3 letter abbreviation).The text was updated successfully, but these errors were encountered: