convert a vector to a list

r

Solution

Like this?

R> kn <- c("1", "a", "b")
R> nl <- vector(mode="list", length=length(kn)-1)
R> names(nl) <- kn[-1]
R> nl <- lapply(nl, function(x) kn[1])
R> nl
$a
[1] "1"

$b
[1] "1"

R> 

With kudos to Gavin for spotting an earlier error.

Problem

I have a vector like this ``` c("1", "a","b") ``` and I'd like to create this list ``` list("a"=1,"b"=1) ``` is there a way to do it in an "apply" style? Thanks. -k

Original source