Monte Shaffer
2010-May-17 21:51 UTC
[R] Variable variables using R ... e.g., looping over data frames with a numeric separator
Hello, I have programmed in PHP a lot, and wanted to know if anyone figured out Variable variables using R. For example, I have several dataframes of unequal sizes that relate to L treatments (1, 2, 3, 4, 5,6, L) ... in this case L=7 fData.1 unique.1 fit.nls.1 summary.nls.1 fit.var.1 summary.var.1 ..... fData.2 unique.2 fit.nls.2 summary.nls.2 fit.var.2 summary.var.2 ..... fData.L unique.L fit.nls.L summary.nls.L fit.var.L summary.var.L ======== I want to do something like for(i in 1:L-1) { dataStr = gsub(' ','',paste("fData.",i)); dataVar = eval(dataStr); ## GOAL is to grab data frame 'fData.1' and do stuff with it, then in next loop grab data frame 'fData.2' and do stuff with it } ############# in PHP, I would define the string $dataStr = "final.1" and then $dataVar $$dataStr which is a variable variables use. Thanks in advance for any help you can offer or suggest. My current solution is to write code in PHP that generates lots of R code. I would like to do it all in R, so I don't have to rely on another language. monte {x: [[alternative HTML version deleted]]
Dan Davison
2010-May-18 03:17 UTC
[R] Variable variables using R ... e.g., looping over data frames with a numeric separator
Monte Shaffer <monte.shaffer at gmail.com> writes:> Hello, > > I have programmed in PHP a lot, and wanted to know if anyone figured out > Variable variables using R. > > For example, I have several dataframes of unequal sizes that relate to L > treatments (1, 2, 3, 4, 5,6, L) ... in this case L=7You should create a list containing 7 data frames, rather than attempting to identify them with names containing integers. Then you can process your data frames in a for loop, or with lapply etc, and things should generally seem much better. df.list <- list(fData.1, fData.2, fData.3, fData.4, fData.5, fData.6, fData.7) Dan> > fData.1 > unique.1 > fit.nls.1 > summary.nls.1 > fit.var.1 > summary.var.1 > ..... > fData.2 > unique.2 > fit.nls.2 > summary.nls.2 > fit.var.2 > summary.var.2 > ..... > fData.L > unique.L > fit.nls.L > summary.nls.L > fit.var.L > summary.var.L > ========> > I want to do something like > > for(i in 1:L-1) > { > dataStr = gsub(' ','',paste("fData.",i)); > dataVar = eval(dataStr); > ## GOAL is to grab data frame 'fData.1' and do stuff with it, then in next > loop grab data frame 'fData.2' and do stuff with it > > > } > > ############# > > in PHP, I would define the string $dataStr = "final.1" and then $dataVar > $$dataStr which is a variable variables use. > > Thanks in advance for any help you can offer or suggest. My current > solution is to write code in PHP that generates lots of R code. I would > like to do it all in R, so I don't have to rely on another language. > > > monte > > {x: > > [[alternative HTML version deleted]]
Tony Plate
2010-May-18 14:24 UTC
[R] Variable variables using R ... e.g., looping over data frames with a numeric separator
On 05/17/2010 03:51 PM, Monte Shaffer wrote:> for(i in 1:L-1) > { > dataStr = gsub(' ','',paste("fData.",i)); > dataVar = eval(dataStr); > ## GOAL is to grab data frame 'fData.1' and do stuff with it, then in next > loop grab data frame 'fData.2' and do stuff with it > > > } >As Dan Davison said, the more standard R way would be to put all your data frames in a list, then iterate over the list. However, if do want to have your data frames in separate variables, and then get each data frame in a loop similar to the code fragment above, try something like this: for (i in 1:(L-1)) { dataName <- paste("fData.", i, sep="") df <- get(dataName) ... do something with data frame df ... } You can also give additional arguments to get() to tell it where to look (pos=,envir=), and whether to look in parent environments (inherits=TRUE/FALSE). -- Tony Plate
Possibly Parallel Threads
- try (nls stops unexpectedly because of chol2inv error
- size limit of string/parse a string and convert to vector
- [fields] image.plot abends with NAs in image.plot.info
- Please help me understand how arrays are translated in rails
- Problems executing cor(dataset) function in R 2.11.0 for OS X ( It works fine in R 2.10.1)