purrr icon indicating copy to clipboard operation
purrr copied to clipboard

map_dfr

Open ggrothendieck opened this issue 1 year ago • 5 comments

map_dfr has been deprecated and the help file suggests using map with list_rbind or list_cbind but that means that we need two commands where we used to get away with one. map_dfr was probably the most frequently used command in purrr at least for me. The reason to deprecate it cited is inconsistency of the name but if that is the only problem we could have map_dfr under another name. Also map(...) %>% list_cbind and map(...) %>% list_cbind do not even work below whereas map_dfr does -- maybe I am doing this wrong but it seems that this is what the help file is suggesting.

# https://stackoverflow.com/questions/76094233/remove-all-non-number-characters-from-every-cell-in-a-df/76094508#76094508
library(purrr)

DF <- structure(list(`Column A` = "dfd:13", `Column B` = "hhh:34", 
    `Column C` = "dsd:15", `Column D` = "ffd:67", `Column E` = "hdf:89", 
    `Column F` = "hhj:43"), class = "data.frame", row.names = c(NA, 
-1L))

DF %>%
  map(trimws, whitespace = "\\D") %>%
  list_cbind
## Error in `list_cbind()`:
## ! Each element of `x` must be either a data frame or `NULL`.
## ℹ Elements 1, 2, 3, 4, 5, and 6 are not.
## Run `rlang::last_trace()` to see where the error occurred.

DF %>%
  map(trimws, whitespace = "\\D") %>%
  list_rbind
## Error in `list_rbind()`:
## ! Each element of `x` must be either a data frame or `NULL`.
## ℹ Elements 1, 2, 3, 4, 5, and 6 are not.
## Run `rlang::last_trace()` to see where the error occurred.

# desired result
DF %>%
  map_dfr(trimws, whitespace = "\\D")
## # A tibble: 1 × 6
##   `Column A` `Column B` `Column C` `Column D` `Column E` `Column F`
##   <chr>      <chr>      <chr>      <chr>      <chr>      <chr>     
## 1 13         34         15         67         89         43   

ggrothendieck avatar Apr 25 '23 14:04 ggrothendieck