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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -