R question on symbolic variables
2
0
Entering edit mode
Chris Fenton ▴ 80
@chris-fenton-3854
Last seen 10.2 years ago
Norway
Is this a limitation in the R language or am I missing something fundamental (like an operator) > a <- list("hello"<-c(1,2,3), "goodbye"<-c(1,2,3)) > a$hello [1] 1 2 3 > b <- "hello" > a$b NULL So how could we do the following for example. > info <- names(a) > for (i in 1:length(info)){cat(a$info[i]);} Chris
• 1.3k views
ADD COMMENT
0
Entering edit mode
Axel Klenk ★ 1.0k
@axel-klenk-3224
Last seen 1 hour ago
UPF, Barcelona, Spain
Dear Chris, the latter. I think you want: a <- list(hello = c(1,2,3), goodbye = c(1,2,3)) b <- "hello" a[b] a[[b]] Note the difference between extracting a sublist using '[' and a list element using '[[' and see: ?"[" Cheers, - axel Axel Klenk Research Informatician Actelion Pharmaceuticals Ltd / Gewerbestrasse 16 / CH-4123 Allschwil / Switzerland Chris Fenton <chrisf at="" fagmed.ui="" t.no=""> To Sent by: bioconductor bioconductor-boun <bioconductor at="" stat.math.ethz.ch=""> ces at stat.math.eth cc z.ch Subject [BioC] R question on symbolic 01/14/2010 09:54 variables AM Is this a limitation in the R language or am I missing something fundamental (like an operator) > a <- list("hello"<-c(1,2,3), "goodbye"<-c(1,2,3)) > a$hello [1] 1 2 3 > b <- "hello" > a$b NULL So how could we do the following for example. > info <- names(a) > for (i in 1:length(info)){cat(a$info[i]);} Chris _______________________________________________ Bioconductor mailing list Bioconductor at stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/bioconductor Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor The information of this email and in any file transmitted with it is strictly confidential and may be legally privileged. It is intended solely for the addressee. If you are not the intended recipient, any copying, distribution or any other use of this email is prohibited and may be unlawful. In such case, you should please notify the sender immediately and destroy this email. The content of this email is not legally binding unless confirmed by letter. Any views expressed in this message are those of the individual sender, except where the message states otherwise and the sender is authorised to state them to be the views of the sender's company. For further information about Actelion please see our website at http://www.actelion.com
ADD COMMENT
0
Entering edit mode
@joern-toedling-3465
Last seen 10.3 years ago
Hello, strictly speaking, this is not a Bioconductor-related question, and most basic R books that describe the "list" class will probably contain the answer, i.e. will describe how the "$" operator works for lists. If you want to use a variable containing the name, use the square brackets: > a[[b]] In addition, for creating the list the correct syntax would be: a <- list("hello"=c(1,2,3), "goodbye"=c(1,2,3)) # '=' instead of '<-' And for loops over lists, you may want to have a look at the functions 'lapply' and 'sapply'. Regards, Joern On Thu, 14 Jan 2010 09:54:14 +0100, Chris Fenton wrote > Is this a limitation in the R language or am I missing something > fundamental (like an operator) > > > a <- list("hello"<-c(1,2,3), "goodbye"<-c(1,2,3)) > > a$hello > [1] 1 2 3 > > > b <- "hello" > > a$b > NULL > > So how could we do the following for example. > > > info <- names(a) > > for (i in 1:length(info)){cat(a$info[i]);} > > Chris
ADD COMMENT

Login before adding your answer.

Traffic: 380 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6