I would like to merge two parts of words to get a name of the data. First M3$N (invariable) and second is a number from 0001 to 3003 - M3$N0001,M3$N0002,...,M3$N3003. For example if I do it like this: my.data <- paste("M3$N",2456,sep="") I get> my.data[1] "M3$N2456" But I want to get something equivalent to my.data<- M3$N2456 Is there any way to do it? It's important, because the second part are randomly chosen. (The data that I would like to extract are in package Mcomp.) Thanks in advance, Rob
On 2/11/2007 3:39 PM, Robert McFadden wrote:> I would like to merge two parts of words to get a name of the data. First > M3$N (invariable) and second is a number from 0001 to 3003 - > M3$N0001,M3$N0002,...,M3$N3003. For example if I do it like this: > my.data <- paste("M3$N",2456,sep="") > I get >> my.data > [1] "M3$N2456" > But I want to get something equivalent to > my.data<- M3$N2456 > > Is there any way to do it? > It's important, because the second part are randomly chosen. > (The data that I would like to extract are in package Mcomp.)You can use eval(parse(text=my.data)) if my.data holds an expression (like "M3$N2456") in text form. Note that M3$N2456 is *not* a name in R; it's an expression saying to extract the component named N2456 from the object named M3. Duncan Murdoch
> -----Original Message----- > From: Duncan Murdoch [mailto:murdoch w stats.uwo.ca] > > eval(parse(text=my.data)) >I would like to thank everybody very much for help, but especially for Duncan - it works wonderful. Rob
Try this: my.data <- M3[[sprintf("N%04d", 1)]] On 2/11/07, Robert McFadden <robert-mcfadden at o2.pl> wrote:> I would like to merge two parts of words to get a name of the data. First > M3$N (invariable) and second is a number from 0001 to 3003 - > M3$N0001,M3$N0002,...,M3$N3003. For example if I do it like this: > my.data <- paste("M3$N",2456,sep="") > I get > > my.data > [1] "M3$N2456" > But I want to get something equivalent to > my.data<- M3$N2456 > > Is there any way to do it? > It's important, because the second part are randomly chosen. > (The data that I would like to extract are in package Mcomp.) > > Thanks in advance, > Rob > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >