[R] Variable variables using R ... e.g., looping over data frames with a numeric separator

Dan Davison davison at stats.ox.ac.uk
Tue May 18 05:17:13 CEST 2010


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=7

You 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]]



More information about the R-help mailing list