Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

appending to a list with dynamic names

Tags:

list

append

r

names

I have a list in R:

a <- list(n1 = "hi", n2 = "hello") 

I would like to append to this named list but the names must be dynamic. That is, they are created from a string (for example: paste("another","name",sep="_")

I tried doing this, which does not work:

c(a, parse(text="paste(\"another\",\"name\",sep=\"_\")=\"hola\"") 

What is the correct way to do this? The end goal is just to append to this list and choose my names dynamically.

like image 953
Alex Avatar asked May 16 '12 04:05

Alex


1 Answers

You could just use indexing with double brackets. Either of the following methods should work.

a <- list(n1 = "hi", n2 = "hello") val <- "another name" a[[val]] <- "hola" a #$n1 #[1] "hi" # #$n2 #[1] "hello" # #$`another name` #[1] "hola"   a[[paste("blah", "ok", sep = "_")]] <- "hey"  a #$n1 #[1] "hi" # #$n2 #[1] "hello" # #$`another name` #[1] "hola" # #$blah_ok #[1] "hey" 
like image 179
Dason Avatar answered Sep 25 '22 04:09

Dason