# Re: [R] To draw observation [m, n] from 20 data frames named frame1, ... , frame20

From: Ray Brownrigg <Ray.Brownrigg_at_mcs.vuw.ac.nz>
Date: Tue, 27 May 2008 11:54:06 +1200

There is another way (isn't there always with R?):

for(i in 1:20) {
vect[i] <- get(paste("frame",i,sep=""))[1, 2] }

get() is your friend, and tidier than eval(parse()).

HTH
Ray Brownrigg

On Tue, 27 May 2008, Peter Dalgaard wrote:
> Gaurav Ghosh wrote:
> > Hi all,
> >
> > I have 20 data frames, named frame1, ... , frame20. Each data frame has
> > the dimension 20x5. I need to build a function that calls out the
> > observation in Row 1, Col 2 from all 20 data frames and places it in a
> > vector of length 20. I have tried the following code:
> >
> > vect <- rep(0,20)
> > for(i in 1:20){
> > vect[i] <- paste("frame",i,"[1,2]",sep="")
> > }
> >
> > The vector that results is of the form ("frame1[1,2]", ... ,
> > "frame20[1,2]"). Instead, the vector I need has the numerical values
> > that correspond to frame1[1,2], frame2[1,2], ... etc. How should I amend
> > my code?
> >
> > Thanks,
> >
> > Gaurav Ghosh
>
> You need an eval(parse(.....)) step if you want to go that route.
>
> However, it is easier if you have your frames in a list to begin with, say
>
> mylist <- mget(sprintf(1:20, fmt="frame%d"))
>
> Then it is just
>
> sapply(mylist, "[", 1, 2)

R-help_at_r-project.org 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. Received on Mon 26 May 2008 - 23:58:06 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 27 May 2008 - 00:30:40 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.