I have some problems with my R data preparation (imported from SPSS), and wasn´t able to find a solution in the web.
Problem: Extract labels for subgroup from data frame:
I have a data frame with 42 variables (columns) and another data frame I made for labels. I generated the label data frame with this code:
labelspss <- read.spss("Ma.sav")
la <- as.data.frame(attr(labelspss, "variable.labels"))
la$`attr(labelspss, "variable.labels")`<- as.character(la$`attr(labelspss, "variable.labels")`)
Column 0 contains the variable names; column 1 contains the labeling from SPSS. Now I want to extract these labels for the subgroup of 42 columns and add it to my data.
I´ve already done it manual for another data frame: created a vector for labels and labeled with package hmisc.
But now 42 variables are too much to do it this way. I think I need something that makes a named character vector out of the label data frame. Something that extracts the variable name from rownames (for defined numbers of rows, like e.g. 140-150) and sets it to the variable label in column 1. I think then I could use label() from the Hmisc package and do the rest.
Thanks for any help!
df <- read_spss(Ma.sav)and if its already labelled in spss you can swap all labels withlabelled_spss(df). I am sorry if that doesn't answer your question, its just a comment. - Stephan