From 05ddff88cbff2e76d0a4182c0c412ade65385d64 Mon Sep 17 00:00:00 2001 From: Maike Date: Thu, 15 Aug 2024 20:44:10 +0200 Subject: [PATCH 1/3] Add example to map_dfr()/map_dfc() Example to demonstrate an edge case, where map_dfr() works, but can not be replaced by list_rbind(). Fixes #1074 --- R/superseded-map-df.R | 31 +++++++++++++++++++++++++++++++ man/map_dfr.Rd | 27 +++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/R/superseded-map-df.R b/R/superseded-map-df.R index 065aa9c9..a0df6a72 100644 --- a/R/superseded-map-df.R +++ b/R/superseded-map-df.R @@ -41,6 +41,32 @@ #' map(\(mod) as.data.frame(t(as.matrix(coef(mod))))) |> #' list_rbind() #' +#' # map: combining vectors --------------------------- +#' DF <- structure( +#' list( +#' `Column A` = c(" 13", " 15 "), +#' `Column B` = c(" 34", " 67 ") +#' ), +#' class = c("data.frame"), +#' row.names = c(NA, -2L) +#' ) +#' +#' # Was: +#' # map_dfr() and map_dfc() actually both combine the list by column +#' map_dfr(DF, trimws) +#' map_dfc(DF, trimws) +#' +#' # Now: +#' # to combine a list of vectors use as_tibble() +#' map(DF, trimws) |> tibble::as_tibble() +#' +#' # list_rbind()/list_cbind() require list of data.frames or NULL to work and +#' # will throw an error +#' \dontrun{ +#' map(DF, trimws) |> list_rbind() +#' map(DF, trimws) |> list_cbind() +#' } +#' #' # map2 --------------------------------------------- #' #' ex_fun <- function(arg1, arg2){ @@ -59,6 +85,11 @@ #' map2_dfc(arg1, arg2, ex_fun) #' # now #' map2(arg1, arg2, ex_fun) |> list_cbind() +#' + + + + map_dfr <- function(.x, .f, ..., .id = NULL) { # in 1.0.0 lifecycle::signal_stage("superseded", "map_dfr()", I("`map()` + `list_rbind()`")) diff --git a/man/map_dfr.Rd b/man/map_dfr.Rd index 3274f223..d4f17e3d 100644 --- a/man/map_dfr.Rd +++ b/man/map_dfr.Rd @@ -71,6 +71,32 @@ mtcars |> map(\(mod) as.data.frame(t(as.matrix(coef(mod))))) |> list_rbind() +# map: combining vectors --------------------------- +DF <- structure( +list( + `Column A` = c(" 13", " 15 "), + `Column B` = c(" 34", " 67 ") +), +class = c("data.frame"), +row.names = c(NA, -2L) +) + +# Was: +# map_dfr() and map_dfc() actually both combine the list by column +map_dfr(DF, trimws) +map_dfc(DF, trimws) + +# Now: +# to combine a list of vectors use as_tibble() +map(DF, trimws) |> tibble::as_tibble() + +# list_rbind()/list_cbind() require list of data.frames or NULL to work and +# will throw an error +\dontrun{ + map(DF, trimws) |> list_rbind() + map(DF, trimws) |> list_cbind() +} + # map2 --------------------------------------------- ex_fun <- function(arg1, arg2){ @@ -89,5 +115,6 @@ map2(arg1, arg2, ex_fun) |> list_rbind() map2_dfc(arg1, arg2, ex_fun) # now map2(arg1, arg2, ex_fun) |> list_cbind() + } \keyword{internal} From 306727cd2370ef73a629bd3c74e6d702886cf0f4 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Tue, 20 Aug 2024 15:49:34 -0500 Subject: [PATCH 2/3] Tweak example --- R/superseded-map-df.R | 35 ++++++++++++++--------------------- man/map_dfr.Rd | 35 ++++++++++++++--------------------- 2 files changed, 28 insertions(+), 42 deletions(-) diff --git a/R/superseded-map-df.R b/R/superseded-map-df.R index a0df6a72..7d527a5e 100644 --- a/R/superseded-map-df.R +++ b/R/superseded-map-df.R @@ -41,31 +41,24 @@ #' map(\(mod) as.data.frame(t(as.matrix(coef(mod))))) |> #' list_rbind() #' -#' # map: combining vectors --------------------------- -#' DF <- structure( -#' list( -#' `Column A` = c(" 13", " 15 "), -#' `Column B` = c(" 34", " 67 ") -#' ), -#' class = c("data.frame"), -#' row.names = c(NA, -2L) +#' # for certain pathological inputs `map_dfr()` and `map_dfc()` actually +#' # both combine the list by column +#' df <- data.frame( +#' x = c(" 13", " 15 "), +#' y = c(" 34", " 67 ") #' ) #' #' # Was: -#' # map_dfr() and map_dfc() actually both combine the list by column -#' map_dfr(DF, trimws) -#' map_dfc(DF, trimws) +#' map_dfr(df, trimws) +#' map_dfc(df, trimws) #' -#' # Now: -#' # to combine a list of vectors use as_tibble() -#' map(DF, trimws) |> tibble::as_tibble() -#' -#' # list_rbind()/list_cbind() require list of data.frames or NULL to work and -#' # will throw an error -#' \dontrun{ -#' map(DF, trimws) |> list_rbind() -#' map(DF, trimws) |> list_cbind() -#' } +#' # If you want to apply a function to each column of a data frame +#' # you might instead want to use modify: +#' modify(df, trimws) +#' +#' # list_rbind()/list_cbind() don't work here because they require +#' # data frame inputs +#' try(map(df, trimws) |> list_rbind()) #' #' # map2 --------------------------------------------- #' diff --git a/man/map_dfr.Rd b/man/map_dfr.Rd index d4f17e3d..d92314de 100644 --- a/man/map_dfr.Rd +++ b/man/map_dfr.Rd @@ -71,31 +71,24 @@ mtcars |> map(\(mod) as.data.frame(t(as.matrix(coef(mod))))) |> list_rbind() -# map: combining vectors --------------------------- -DF <- structure( -list( - `Column A` = c(" 13", " 15 "), - `Column B` = c(" 34", " 67 ") -), -class = c("data.frame"), -row.names = c(NA, -2L) +# for certain pathological inputs `map_dfr()` and `map_dfc()` actually +# both combine the list by column +df <- data.frame( + x = c(" 13", " 15 "), + y = c(" 34", " 67 ") ) # Was: -# map_dfr() and map_dfc() actually both combine the list by column -map_dfr(DF, trimws) -map_dfc(DF, trimws) +map_dfr(df, trimws) +map_dfc(df, trimws) -# Now: -# to combine a list of vectors use as_tibble() -map(DF, trimws) |> tibble::as_tibble() - -# list_rbind()/list_cbind() require list of data.frames or NULL to work and -# will throw an error -\dontrun{ - map(DF, trimws) |> list_rbind() - map(DF, trimws) |> list_cbind() -} +# If you want to apply a function to each column of a data frame +# you might instead want to use modify: +modify(df, trimws) + +# list_rbind()/list_cbind() don't work here because they require +# data frame inputs +try(map(df, trimws) |> list_rbind()) # map2 --------------------------------------------- From 88bdb6c74335dc3a6abaa146e3c37304017caa89 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Wed, 21 Aug 2024 08:00:54 -0500 Subject: [PATCH 3/3] More polishing --- R/superseded-map-df.R | 15 ++++----------- man/map_dfr.Rd | 11 ++++------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/R/superseded-map-df.R b/R/superseded-map-df.R index 7d527a5e..8b9fb0d1 100644 --- a/R/superseded-map-df.R +++ b/R/superseded-map-df.R @@ -52,14 +52,12 @@ #' map_dfr(df, trimws) #' map_dfc(df, trimws) #' -#' # If you want to apply a function to each column of a data frame -#' # you might instead want to use modify: -#' modify(df, trimws) -#' -#' # list_rbind()/list_cbind() don't work here because they require -#' # data frame inputs +#' # But list_rbind()/list_cbind() fail because they require data frame inputs #' try(map(df, trimws) |> list_rbind()) #' +#' # Instead, use modify() to apply a function to each column of a data frame +#' modify(df, trimws) +#' #' # map2 --------------------------------------------- #' #' ex_fun <- function(arg1, arg2){ @@ -78,11 +76,6 @@ #' map2_dfc(arg1, arg2, ex_fun) #' # now #' map2(arg1, arg2, ex_fun) |> list_cbind() -#' - - - - map_dfr <- function(.x, .f, ..., .id = NULL) { # in 1.0.0 lifecycle::signal_stage("superseded", "map_dfr()", I("`map()` + `list_rbind()`")) diff --git a/man/map_dfr.Rd b/man/map_dfr.Rd index d92314de..f9c9dd43 100644 --- a/man/map_dfr.Rd +++ b/man/map_dfr.Rd @@ -82,14 +82,12 @@ df <- data.frame( map_dfr(df, trimws) map_dfc(df, trimws) -# If you want to apply a function to each column of a data frame -# you might instead want to use modify: -modify(df, trimws) - -# list_rbind()/list_cbind() don't work here because they require -# data frame inputs +# But list_rbind()/list_cbind() fail because they require data frame inputs try(map(df, trimws) |> list_rbind()) +# Instead, use modify() to apply a function to each column of a data frame +modify(df, trimws) + # map2 --------------------------------------------- ex_fun <- function(arg1, arg2){ @@ -108,6 +106,5 @@ map2(arg1, arg2, ex_fun) |> list_rbind() map2_dfc(arg1, arg2, ex_fun) # now map2(arg1, arg2, ex_fun) |> list_cbind() - } \keyword{internal}