[R] SPSS to R Data file conversion

jim holtman jholtman at gmail.com
Sun Apr 13 02:35:25 CEST 2008


?assign

I think this will work for you.

list=dir(pattern=".sav")
library(foreign)
for (i in 1:length(list)){
    # The saved data frame will be dat
    name=substring(list[i],1,nchar(list[i])-4)
    assign(name, read.spss(list[i],to.data.frame=TRUE))
    rname=paste("../R/",name,".rda",sep="")
    # This uses dat for the saved dataframe rather than name
    save(list=name,file=rname)
}

On Sat, Apr 12, 2008 at 8:17 PM, Robert Baer <rbaer at atsu.edu> wrote:
> I wrote a little routine to convert multiple spss data files (as data frames) to R data files.  The code is as follows:
> #
> list=dir(pattern=".sav")
> library(foreign)
> for (i in 1:length(list)){
> # The saved data frame will be dat
> dat=read.spss(list[i],to.data.frame=TRUE)
> name=substring(list[i],1,nchar(list[i])-4)
> rname=paste("../R/",name,".rda",sep="")
> # This uses dat for the saved dataframe rather than name
> save(dat,file=rname)
> }
>
> This works just fine, but the data frames are all named dat when loaded into Rcmdr. It is not a problem to do an assignment at load time
> from the command line as in catnip=load("catnip.rda"), but Rcmdr uses the data frame name saved in the compressed .rda file upon loading.
>
> My variable "name" contains the string for the dataframe name I would like to use.  I just can't figure out how to assign this name to the dat dataframe in the context of my loop.
>
> Any hints?
>
> Thanks,
> Rob Baer
>
>
>        [[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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list