Gaurav Ghosh
2008-May-26 22:16 UTC
[R] To draw observation [m, n] from 20 data frames named frame1, ... , frame20
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
Graduate Student,
Department of Agricultural Economics and Rural Sociology,
311 Armsby Building,
Pennsylvania State University,
University Park, PA 16802
Ph: 814-865-1128
[[alternative HTML version deleted]]
Peter Dalgaard
2008-May-26 22:34 UTC
[R] To draw observation [m, n] from 20 data frames named frame1, ... , frame20
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) -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907