How can I convert the list of strings a to list of lists b as
a = list('A', 'B', 'C')
and the converted to
b = list(list('A'), list('B'), list('C'))
CodePudding user response:
Apply the function list to the list a:
lapply(a, list)
[[1]]
[[1]][[1]]
[1] "A"
[[2]]
[[2]][[1]]
[1] "B"
[[3]]
[[3]][[1]]
[1] "C"
CodePudding user response:
A possible solution, using purrr::map:
library(purrr)
a = list('A', 'B', 'C')
b = list(list('A'), list('B'), list('C'))
a %>% map( ~ list(.x)) %>% identical(b)
#> [1] TRUE
a %>% map( ~ list(.x))
#> [[1]]
#> [[1]][[1]]
#> [1] "A"
#>
#>
#> [[2]]
#> [[2]][[1]]
#> [1] "B"
#>
#>
#> [[3]]
#> [[3]][[1]]
#> [1] "C"
Or simply:
map(a, list)
CodePudding user response:
Another base R option:
Map(list, a)
Output
[[1]]
[[1]][[1]]
[1] "A"
[[2]]
[[2]][[1]]
[1] "B"
[[3]]
[[3]][[1]]
[1] "C"
Benchmark
As suspected, lapply is the fastest.

