[R] referencing a data frame with a pointer, link, alias, etc
Jim Lemon
jim at bitwrit.com.au
Fri May 30 01:00:15 CEST 2014
On Thu, 29 May 2014 01:13:10 PM dansawyer wrote:
> Good afternoon.
>
> The question is: does R support pointer like structures? The specific
> problem is:
>
> a function supporting operations on a set of data frames with similar
> structures.
>
> I have a set of data frames with similar structures. I wish to perform
> similar operations on those data frames. I have successfully created
and
> tested the operations and am ready to write a local function to
perform
> them. To clarify the question below is a pigeon example for the
question:
>
> df1 <- (col1, col2, ...)
> df2 <- (col1, col2, ...)
>
> function f(var)
> {var$col1 <- ...
> var$col2 <- ...
> }
>
> f(df1)
>
> Each of the data frames have common column names
>
> I would like for f to be able to perform operations on the data frames
from
> within the function. I have tried testing variables without success. R
> allows for creating the necessary 'strings' with the correct names,
however
> I am unable to bind, transform those strings to reference the data
frames.
>
>
Hi dansawyer,
I may not completely understand what you want to do, but I think you
want the "get" function to sequentially process your dataframes.
Maybe something like this:
for(df in paste("df",1:10,sep="")) {
var<-get(df)
#do your processing on "var"
}
If you want to read in say, CSV files one at a time:
for(df in paste("df",1:10,".csv",sep="")) {
var<-read.csv(df)
#do your processing on "var"
}
You may also want to define a class that specifies the structure of your
dataframes.
Jim
More information about the R-help
mailing list