[R] load() into a data frame with my chosen name => .ThisEnv ?
Neal Fultz
nfultz at ucla.edu
Tue Jun 18 03:49:02 CEST 2013
For your problem, you might want to use saveRDS and readRDS instead of
save and load. I would also use dir to get a list of files rather
than paste.
Then your solution might look like:
run <- function(filename) {
df <- readRDS(filename);
coef( lm(df[,1] ~ df[,2]) )
}
results <- lapply( dir(pattern=glob2rx('d*.rds')), run)
If this is disk-bound, using multiple threads probably won't help much.
You'll have to try it out for yourself.
-nfultz
On Mon, Jun 17, 2013 at 04:07:50PM -0700, ivo welch wrote:
> very simple question. I probably forgot the answer (early stage onset),
> and I could not find it via google and r-help.
>
> I have a number of files, each containing one saved data frame. I want to
> do the equivalent of
>
> d <- .GlobalEnv[[ load(file="dxxxx.Rdata") ]
>
> but inside a function, and I don't need the original name (dxxxx). in the
> end, I want to do something like
>
> run <- function( i ) {
> fname <- paste0( "d", i, ".Rdata")
> stopifnot( length(fname)==1 )
> d <- .ThisEnv[[ load(file=fname) ]]
> coef( lm(d[,1] ~ d[,2]) ) ## calculate two coefficients and return
> them
> }
>
> results <- mclapply( 1:10000, run )
>
> stumped over something that should be easy...pointer appreciated.
>
> /iaw
> ----
> Ivo Welch (ivo.welch at gmail.com)
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
More information about the R-help
mailing list