Convert to List with Names Equal to List Elements

toNamedList(x)

Arguments

x

R object, preferably vector of character

Value

result of calling as.list on x with list element names being set to the elements of x.

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" #>