I have a list
myList = list(a = 1, b = 2)
names(myList)
# [1] "a" "b"
I want to select element from 'myList' by name stored in as string.
for (name in names(myList)){
print (myList$name)
}
This is not working because name = "a", "b". My selecting line actually saying myList$"a" and myList$"b". I also tried:
print(myList$get(name))
print(get(paste(myList$, name, sep = "")))
but didn't work. Thank you very much if you could tell me how to do it.
myList[[name]]. Also,printrequires parentheses in R, likeprint(x)notprint x.myList[[name]]paste(myList$, name, sep = "")is not valid R syntax. You need to quote all non-variable elements.