michael watson (IAH-C)
2005-Feb-10 13:35 UTC
[R] Using a number as a name to access a list
Hi Dumb question time again, for which I apologise. I have a variable that contains the following numerical text "04010". This is the name to access a list:> as.list(KEGGPATHID2NAME)$"04010"[1] "MAPK signaling pathway" Marvellous! Except I want to do that when "04010" is assigned to a variable called path and I can't figure out how to do it!> path <- "04010" > > # the original and best > as.list(KEGGPATHID2NAME)$"04010"[1] "MAPK signaling pathway"> > # clearly this doesn't, and shouldn't, work > as.list(KEGGPATHID2NAME)$pathNULL> > # this produces a string... > eval(paste("as.list(KEGGPATHID2NAME)$",path,sep=''))[1] "as.list(KEGGPATHID2NAME)$04010"> > # as does this > eval(paste('as.list(KEGGPATHID2NAME)$"',path,'"',sep=''))[1] "as.list(KEGGPATHID2NAME)$\"04010\"">Whats really annoying is that when everyone mails me the answer, I'm going to have known how obvious it is.... Thanks in advance Mick Village Idiot
michael watson (IAH-C)
2005-Feb-10 13:40 UTC
[R] RE: Using a number as a name to access a list
The answer of course is parse()! Thank you and good night! M -----Original Message----- From: michael watson (IAH-C) Sent: 10 February 2005 13:36 To: r-help at stat.math.ethz.ch Subject: Using a number as a name to access a list Hi Dumb question time again, for which I apologise. I have a variable that contains the following numerical text "04010". This is the name to access a list:> as.list(KEGGPATHID2NAME)$"04010"[1] "MAPK signaling pathway" Marvellous! Except I want to do that when "04010" is assigned to a variable called path and I can't figure out how to do it!> path <- "04010" > > # the original and best > as.list(KEGGPATHID2NAME)$"04010"[1] "MAPK signaling pathway"> > # clearly this doesn't, and shouldn't, work > as.list(KEGGPATHID2NAME)$pathNULL> > # this produces a string... > eval(paste("as.list(KEGGPATHID2NAME)$",path,sep=''))[1] "as.list(KEGGPATHID2NAME)$04010"> > # as does this > eval(paste('as.list(KEGGPATHID2NAME)$"',path,'"',sep=''))[1] "as.list(KEGGPATHID2NAME)$\"04010\"">Whats really annoying is that when everyone mails me the answer, I'm going to have known how obvious it is.... Thanks in advance Mick Village Idiot
lists are like vectors, i.e., x <- c("a" = 1, "b" = 2) path <- "b" x[path] the same applies to lists: lis <- list(a = 1:3) path <- "a" lis[path] I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "michael watson (IAH-C)" <michael.watson at bbsrc.ac.uk> To: <r-help at stat.math.ethz.ch> Sent: Thursday, February 10, 2005 2:35 PM Subject: [R] Using a number as a name to access a list> Hi > > Dumb question time again, for which I apologise. > > I have a variable that contains the following numerical text > "04010". > This is the name to access a list: > >> as.list(KEGGPATHID2NAME)$"04010" > [1] "MAPK signaling pathway" > > Marvellous! Except I want to do that when "04010" is assigned to a > variable called path and I can't figure out how to do it! > >> path <- "04010" >> >> # the original and best >> as.list(KEGGPATHID2NAME)$"04010" > [1] "MAPK signaling pathway" >> >> # clearly this doesn't, and shouldn't, work >> as.list(KEGGPATHID2NAME)$path > NULL >> >> # this produces a string... >> eval(paste("as.list(KEGGPATHID2NAME)$",path,sep='')) > [1] "as.list(KEGGPATHID2NAME)$04010" >> >> # as does this >> eval(paste('as.list(KEGGPATHID2NAME)$"',path,'"',sep='')) > [1] "as.list(KEGGPATHID2NAME)$\"04010\"" >> > > Whats really annoying is that when everyone mails me the answer, I'm > going to have known how obvious it is.... Thanks in advance > > Mick > Village Idiot > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
On Thu, Feb 10, 2005 at 01:35:56PM -0000, michael watson (IAH-C) wrote:> I have a variable that contains the following numerical text "04010". > This is the name to access a list: > > > as.list(KEGGPATHID2NAME)$"04010" > [1] "MAPK signaling pathway" > > Marvellous! Except I want to do that when "04010" is assigned to a > variable called path and I can't figure out how to do it! > > > path <- "04010" > > > > # the original and best > > as.list(KEGGPATHID2NAME)$"04010" > [1] "MAPK signaling pathway" > > > > # clearly this doesn't, and shouldn't, work > > as.list(KEGGPATHID2NAME)$path > NULL$ allows only a literal character string or a symbol as the index, according to the R language definition, but [[ indexing does the same as $ and can be used for computed indexing. So as.list(KEGGPATHID2NAME)[[path]] should do what you want. Greetinx, Jan -- +- Jan T. Kim -------------------------------------------------------+ | *NEW* email: jtk at cmp.uea.ac.uk | | *NEW* WWW: http://www.cmp.uea.ac.uk/people/jtk | *-----=< hierarchical systems are for files, not for humans >=-----*