Argument 1 must have names - purrr::map_df

Hi, I get this error: Argument 1 must have names

here is the code:

expand_grid(x = c(1, 3, 9), y = c(4, 6, 2)) %>% 
  pmap(sum)

It works, but I wanted to have a dataframe as an output, so I have done:

expand_grid(x = c(1, 3, 9), y = c(4, 6, 2)) %>% 
  pmap_df(sum)

doesn't work with error:
Error: Argument 1 must have names.
So next, I have done:

expand_grid(x = c(1, 3, 9), y = c(4, 6, 2)) %>% as.data.frame(.) %>% set_names(paste0('V', 1:2)) %>%  pmap_df(.,sum) 

but still the same error:

Error: Argument 1 must have names.
Run `rlang::last_error()` to see where the error occurred.

and:

rlang::last_error()
<error/rlang_error>
Argument 1 must have names.
Backtrace:
 1. `%>%`(...)
 2. purrr::pmap_df(., sum)
 3. dplyr::bind_rows(res, .id = .id)
Run `rlang::last_trace()` to see the full context.

and:

rlang::last_trace()
<error/rlang_error>
Argument 1 must have names.
Backtrace:
    x
 1. +-`%>%`(...)
 2. \-purrr::pmap_df(., sum)
 3.   \-dplyr::bind_rows(res, .id = .id)

but this works:

expand_grid(x = c(1, 3, 9), y = c(4, 6, 2)) %>% 
  pmap_dfc(sum)

Why is that, could anybody explain, please ?
My desired output woud be having three variables: V1, V2 and V3 as sum in dataframe.

Do you mean two variables, because your expand_grid() only returns x and y.

# two variables x and y summed
expand_grid(x = c(1, 3, 9), y = c(4, 6, 2)) %>% 
  map_df(~sum(.))

# A tibble: 1 x 2
      x     y
  <dbl> <dbl>
1    39    36
expand_grid(x = c(1, 3, 9), y = c(4, 6, 2)) %>%  pmap_df(
~ tibble(x = .x, y = .y), 
.id = "ID") %>% rowwise() %>% mutate( results_of_addition = sum(x+y)) 

Thank you @williaml , I was rather thinking about additional column like in this example above, wondering if it could be written better (shorter, nicer) as this:

expand_grid(x = c(1, 3, 9), y = c(4, 6, 2)) %>% pmap(sum)

gives me that additional column but as a list not a dataframe.
But thank you for pointing me out as well to:

 map_df(~sum(.))

as I knew that somewhere I should have placed a tilde.
I am learning purrr and trying to understand where this error:

Argument 1 must have names

comes from.

I am also not sure why the pmap_dfr gives an error and pmao_dfc only a warning.
In the latter case a 'solution' is found by assigning columnnames ...1 etc.
The name ...1 could also have been assigned to a one-column result
Probably I am missing something here (?)

library(purrr)
library(tidyr)
z <- tidyr::expand_grid(x = c(1, 3, 9), y = c(4, 6, 2)) 
str(z)
#> tibble [9 x 2] (S3: tbl_df/tbl/data.frame)
#>  $ x: num [1:9] 1 1 1 3 3 3 9 9 9
#>  $ y: num [1:9] 4 6 2 4 6 2 4 6 2
z1 <- z %>% pmap(sum)
str(z1)
#> List of 9
#>  $ : num 5
#>  $ : num 7
#>  $ : num 3
#>  $ : num 7
#>  $ : num 9
#>  $ : num 5
#>  $ : num 13
#>  $ : num 15
#>  $ : num 11
z2 <- z %>% pmap_dfc(sum)
#> New names:
#> * NA -> ...1
#> * NA -> ...2
#> * NA -> ...3
#> * NA -> ...4
#> * NA -> ...5
#> * ...
str(z2)
#> tibble [1 x 9] (S3: tbl_df/tbl/data.frame)
#>  $ ...1: num 5
#>  $ ...2: num 7
#>  $ ...3: num 3
#>  $ ...4: num 7
#>  $ ...5: num 9
#>  $ ...6: num 5
#>  $ ...7: num 13
#>  $ ...8: num 15
#>  $ ...9: num 11
z %>% pmap_dfr(sum)
#> Error: Argument 1 must have names.
Created on 2021-09-05 by the reprex package (v2.0.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.