Home > database >  How to resolve error: Argument 1 must have names when using map( ) pluck( ) in R to list( )?
How to resolve error: Argument 1 must have names when using map( ) pluck( ) in R to list( )?

Time:03-04

I have this:

list(list(result="0001"),list(result="0002")) %>%
purrr::map(pluck, 'result') %>% 
dplyr::bind_rows() 

output: Error: Argument 1 must have names.

The error occurs because the result in the list result "0001" and "0002" have the same name [1], output:

[[1]]
[[1]]$result
[1] "0001" <---- [1]

[[2]]
[[2]]$result
[1] "0002" <---- [1]

How to restart dianically for n cases the name? Expected Output:

[[1]]
[[1]]$result
[1] "0001" <---- [1]

[[2]]
[[2]]$result
[2] "0002" <---- [2]

Possibly it must be some transform or function between map() and bind_rows()

purrr::map(pluck, 'result') %>%
???? 
dplyr::bind_rows() 

CodePudding user response:

To bind_rows(), you just need to convert the list of vectors into a list of dataframe beforehand. So, you could do:

Reprex

  • Code
library(purrr)
library(dplyr)

list(list(result="0001"),list(result="0002")) %>%
  purrr::map(pluck, 'result') %>% 
  purrr::map(., ~ data.frame(results = .)) %>% 
  bind_rows()
  • Output
#>   results
#> 1    0001
#> 2    0002

Created on 2022-03-04 by the reprex package (v2.0.1)

  • Related