Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create list programmatically with tags from character vector

Tags:

r

I am probably going to knock my head against the table because its obvious but how do you create a list programatically from a character vector such that the character vector provides the tags and a nother vector the values. E.g.

character.vector <- c('first.element', 'second.element')
values.vector <- c(1, 2)
a.list <- list(//magic here//)
print(a.list) // prints the same as list(first.element=1, second.element=2)
like image 991
joidegn Avatar asked Jun 18 '13 12:06

joidegn


2 Answers

character.vector <- c('first.element', 'second.element')
values.vector <- c(1, 2)

as.list(setNames(values.vector, character.vector))
like image 61
user1609452 Avatar answered Sep 19 '22 15:09

user1609452


You could set:

   > names(values.vector) <- character.vector
   > values.vector
     first.element second.element 
                 1              2

and, of course, convert it into a list if necessary:

> as.list(values.vector)
$first.element
[1] 1

$second.element
[1] 2
like image 43
harkmug Avatar answered Sep 19 '22 15:09

harkmug