Dear R-Help list, I have a problem with convertions of strings. I want to use the function "paste()" to create an object name and then use that character string to call on that object. So, for example: dat99 <- matrix(rbind(1,1,2),3,3) no <- 99 dat <- paste("dat",no,sep="") dat [1] "dat99" What should I do to get the output dat [,1] [,2] [,3] [1,] 1 1 1 [2,] 1 1 1 [3,] 2 2 2 Cheers, Gudrun -- Gudrun Jonasdottir, M.Sc. Matematiska institutionen Stockholms Universitet SE- 106 91 Stockholm Work: +46 (0)8 16 45 56 Mobile: +46 (0)709 779 800
See ?get. Andy> From: Gudrun Jonasdottir > > Dear R-Help list, > > I have a problem with convertions of strings. I want to use > the function > "paste()" to create an object name and then use that > character string to > call on that object. So, for example: > > dat99 <- matrix(rbind(1,1,2),3,3) > no <- 99 > dat <- paste("dat",no,sep="") > dat > [1] "dat99" > > What should I do to get the output > > dat > [,1] [,2] [,3] > [1,] 1 1 1 > [2,] 1 1 1 > [3,] 2 2 2 > > Cheers, > Gudrun > > > > -- > Gudrun Jonasdottir, M.Sc. > Matematiska institutionen > Stockholms Universitet > SE- 106 91 Stockholm > > Work: +46 (0)8 16 45 56 > Mobile: +46 (0)709 779 800 > > ______________________________________________ > 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 > >
Hi Gudrun, try to use eval(parse(text=dat)) Best, Dimitris ---- Dimitris Rizopoulos Doctoral Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/396887 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Gudrun Jonasdottir" <gudrunj at math.su.se> To: <r-help at stat.math.ethz.ch> Sent: Thursday, August 19, 2004 4:15 PM Subject: [R] convert strings to object names> Dear R-Help list, > > I have a problem with convertions of strings. I want to use thefunction> "paste()" to create an object name and then use that characterstring to> call on that object. So, for example: > > dat99 <- matrix(rbind(1,1,2),3,3) > no <- 99 > dat <- paste("dat",no,sep="") > dat > [1] "dat99" > > What should I do to get the output > > dat > [,1] [,2] [,3] > [1,] 1 1 1 > [2,] 1 1 1 > [3,] 2 2 2 > > Cheers, > Gudrun > > > > -- > Gudrun Jonasdottir, M.Sc. > Matematiska institutionen > Stockholms Universitet > SE- 106 91 Stockholm > > Work: +46 (0)8 16 45 56 > Mobile: +46 (0)709 779 800 > > ______________________________________________ > 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
Gudrun Jonasdottir wrote:> Dear R-Help list, > > I have a problem with convertions of strings. I want to use the function > "paste()" to create an object name and then use that character string to > call on that object. So, for example: > > dat99 <- matrix(rbind(1,1,2),3,3) > no <- 99 > dat <- paste("dat",no,sep="") > dat > [1] "dat99" > > What should I do to get the outputget(dat) Uwe Ligges> dat > [,1] [,2] [,3] > [1,] 1 1 1 > [2,] 1 1 1 > [3,] 2 2 2 > > Cheers, > Gudrun > > >
assign("dat", get(paste("dat", no, sep=""))) or simply dat <- get(paste("dat", no, sep="")) On Thu, 2004-08-19 at 15:15, Gudrun Jonasdottir wrote:> Dear R-Help list, > > I have a problem with convertions of strings. I want to use the function > "paste()" to create an object name and then use that character string to > call on that object. So, for example: > > dat99 <- matrix(rbind(1,1,2),3,3) > no <- 99 > dat <- paste("dat",no,sep="") > dat > [1] "dat99" > > What should I do to get the output > > dat > [,1] [,2] [,3] > [1,] 1 1 1 > [2,] 1 1 1 > [3,] 2 2 2 > > Cheers, > Gudrun > >
And this is all in the FAQ. -thomas On Thu, 19 Aug 2004, Adaikalavan Ramasamy wrote:> assign("dat", get(paste("dat", no, sep=""))) > > or simply > > dat <- get(paste("dat", no, sep="")) > > > On Thu, 2004-08-19 at 15:15, Gudrun Jonasdottir wrote: > > Dear R-Help list, > > > > I have a problem with convertions of strings. I want to use the function > > "paste()" to create an object name and then use that character string to > > call on that object. So, for example: > > > > dat99 <- matrix(rbind(1,1,2),3,3) > > no <- 99 > > dat <- paste("dat",no,sep="") > > dat > > [1] "dat99" > > > > What should I do to get the output > > > > dat > > [,1] [,2] [,3] > > [1,] 1 1 1 > > [2,] 1 1 1 > > [3,] 2 2 2 > > > > Cheers, > > Gudrun > > > > > > ______________________________________________ > 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 >Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle
Use the get() function: > dat99 <- matrix(c(1,1,2),3,3) > no <- 99 > dat.name <- paste("dat",no,sep="") > get(dat.name) [,1] [,2] [,3] [1,] 1 1 1 [2,] 1 1 1 [3,] 2 2 2 > At 04:15 PM 8/19/2004 +0200, Gudrun Jonasdottir wrote:>Dear R-Help list, > >I have a problem with convertions of strings. I want to use the function >"paste()" to create an object name and then use that character string to >call on that object. So, for example: > >dat99 <- matrix(rbind(1,1,2),3,3) >no <- 99 >dat <- paste("dat",no,sep="") >dat >[1] "dat99" > >What should I do to get the output > >dat > [,1] [,2] [,3] >[1,] 1 1 1 >[2,] 1 1 1 >[3,] 2 2 2 > >Cheers, >Gudrun > > > >-- >Gudrun Jonasdottir, M.Sc. >Matematiska institutionen >Stockholms Universitet >SE- 106 91 Stockholm > >Work: +46 (0)8 16 45 56 >Mobile: +46 (0)709 779 800 > >______________________________________________ >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
Dear R-list, Apologies for replying late. Thanks to Andy, Dimitris, James, Uwe, Olaf, Adai, Gardar and Patrick. (Apologies if I forgot someone) In summary: The quickest way to convert a string to an object name, seems to be by using get(dat), but it seems to work equally well if you use any of eval(parse(text=dat)) do.call("print", list(as.name(dat))) eval(substitute(print(x), list(x=as.name(dat)))) assign("dat", get(paste("dat", no, sep=""))) Thanks again, Gudrun Gardar Johannessonwrote:> Use the get() function: > > > dat99 <- matrix(c(1,1,2),3,3) > > no <- 99 > > dat.name <- paste("dat",no,sep="") > > get(dat.name) > [,1] [,2] [,3] > [1,] 1 1 1 > [2,] 1 1 1 > [3,] 2 2 2 > > > > > At 04:15 PM 8/19/2004 +0200, Gudrun Jonasdottir wrote: >>Dear R-Help list, >> >>I have a problem with convertions of strings. I want to use the function >>"paste()" to create an object name and then use that character string to >>call on that object. So, for example: >> >>dat99 <- matrix(rbind(1,1,2),3,3) >>no <- 99 >>dat <- paste("dat",no,sep="") >>dat >>[1] "dat99" >> >>What should I do to get the output >> >>dat >> [,1] [,2] [,3] >>[1,] 1 1 1 >>[2,] 1 1 1 >>[3,] 2 2 2 >> >>Cheers, >>Gudrun >> >> >> >>-- >>Gudrun Jonasdottir, M.Sc. >>Matematiska institutionen >>Stockholms Universitet >>SE- 106 91 Stockholm >> >>Work: +46 (0)8 16 45 56 >>Mobile: +46 (0)709 779 800 >> >>______________________________________________ >>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 > >