I have a list that I need to collapse into a string, preserving list names in the form name1=value1&name2=value2...
But i'm getting some unexpected behavior in passing arguments for .f to map2. I'm definitely missing something simple.
To be clear, the desired final value from the reprex is "people=amy&age=25&state=NY".
Any help is appreciated.
library(purrr)
library(dplyr) # because I use the pipe in my example
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
# a toy list
my_list <- list(people = "amy", age = 30, state = "NY" )
# DESIRED END RESULT IS A STRING: "people=amy&age=25&state=NY"
# why isn't the collapse argument making it to paste in map2() here?
yy <- my_list %>% map2(.x = names(.), .y = ., .f = paste, collapse = "=")
yy
#> [[1]]
#> [1] "people amy"
#>
#> [[2]]
#> [1] "age 30"
#>
#> [[3]]
#> [1] "state NY"
# this now adds the ampersand as expected
yy %>% unlist() %>% paste(collapse = "&")
#> [1] "people amy&age 30&state NY"
Created on 2018-10-07 by the reprex package (v0.2.1)