[R] Merging dataframe with list

Stefan Petersson stefan.petersson at inizio.se
Tue May 25 11:24:34 CEST 2010


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?

TIA

/s



More information about the R-help mailing list