[R] Merging dataframe with list

Ivan Calandra ivan.calandra at uni-hamburg.de
Tue May 25 11:57:26 CEST 2010


I definitely have no idea of what to do precisely, but there is 
something you should know: data.frames ARE lists but where all elements 
have the same length.
I don't know if that would change anything, but it might help you 
finding an alternative.


Le 5/25/2010 11:24, Stefan Petersson a écrit :
> I often read SPSS system files (*.sav) into R using the 'read.spss' function from the 'foreign' library. To retain all the meta data, i.e. 'variable labels', I call the function like this:
> test.sav<- read.spss('http://www.cdc.gov/healthyYouth/shpps/2006/spss/envs2006.sav',
>   to.data.frame=FALSE,
>   use.value.labels=TRUE
> )
> This returns a list, which is fine. However. How do I merge data to this list, keeping the list components intact? Let's assume I have a dataframe that looks like this:
> df<- data.frame(id=rep(1:100),data=runif(100, min=0, max=1))
> I want to add the column 'df$data' to 'test.sav' by matching the columns 'd$id' with 'test.sav$stcmsid'. If I use 'merge', the resulting class is 'data.frame', hence the variable labels from 'test.sav' will be silently dropped.
> Is there an elegant way to merge data to a list like 'test.sav', or should I just save 'attr(test.sav, "variable.labels")' and be done with it?
> /s
> ______________________________________________
> 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.

PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra at uni-hamburg.de


More information about the R-help mailing list