Dynamically creating named list in R -
i need create named lists dynamically in r follows.
suppose there array of names.
name_arr<-c("a","b")
and there array of values.
value_arr<-c(1,2,3,4,5,6)
what want this:
list(name_arr[1]=value_arr[1:3])
but r throws error when try this. suggestions how around problem?
you use setnames
. examples:
setnames(list(value_arr[1:3]), name_arr[1]) #$a #[1] 1 2 3 setnames(list(value_arr[1:3], value_arr[4:6]), name_arr) #$a #[1] 1 2 3 # #$b #[1] 4 5 6
or without setnames
:
mylist <- list(value_arr[1:3]) names(mylist) <- name_arr[1] mylist #$a #[1] 1 2 3 mylist <- list(value_arr[1:3], value_arr[4:6]) names(mylist) <- name_arr mylist #$a #[1] 1 2 3 # #$b #[1] 4 5 6
Comments
Post a Comment