Skip to content
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

Need to distinguish rows vs. cols #167

Closed
hadley opened this issue Jan 15, 2016 · 5 comments
Closed

Need to distinguish rows vs. cols #167

hadley opened this issue Jan 15, 2016 · 5 comments
Labels
feature a feature request or enhancement

Comments

@hadley
Copy link
Member

hadley commented Jan 15, 2016

Maybe map_dfc() and map_dfr()? (Nice because then all types have a 3 letter abbreviation).

@hadley
Copy link
Member Author

hadley commented Jan 16, 2016

Or have map_df(x, .direction = 1).

@lionel-
Copy link
Member

lionel- commented Jan 18, 2016

Maybe .collate = c("rows", "cols") to be consistent with the rows-based functionals.

@hadley
Copy link
Member Author

hadley commented Sep 21, 2016

I think I'd rather keep these as separate functions: these are special cases because data frames are so important in R.

@jimhester
Copy link
Contributor

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
#> ...

@hadley hadley added the feature a feature request or enhancement label Mar 3, 2017
@hadley
Copy link
Member Author

hadley commented Mar 5, 2017

@jimhester why not use bind_cols() for symmetry?

@hadley hadley closed this as completed in cab4d48 Mar 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants