Convert to List with Names Equal to List Elements
Examples
x.vector <- c("Peter", "Paul", "Mary")
x.list <- toNamedList(x.vector)
all(names(x.list) == x.vector)
#> [1] TRUE
# You may use toNamedList in a call of lapply in order to get a named result
lapply(toNamedList(x.vector), function(x) sprintf("Hello, %s", x))
#> $Peter
#> [1] "Hello, Peter"
#>
#> $Paul
#> [1] "Hello, Paul"
#>
#> $Mary
#> [1] "Hello, Mary"
#>
# Compare with
lapply(x.vector, function(x) sprintf("Hello, %s", x))
#> [[1]]
#> [1] "Hello, Peter"
#>
#> [[2]]
#> [1] "Hello, Paul"
#>
#> [[3]]
#> [1] "Hello, Mary"
#>