#creating dataframe
df1=data.frame(c("Mary","Sarah","Linda","Mark","Shaun","Jo"),c(1,2,3,4,5,6),c(2,2,2,1,1,2))
names(df1)=c("a","b","c")
#setting levels and labels for questions b and c
blevels=c(1,2,3,4,5,6)
blabels=c("bike","walk","car","bus","train","subway")
clevels=c(1,2)
clabels=c("male","female")
#creating labelled data.frame
df2=df1
#applying labels
df2$b<-factor(df1$b,blevels,blabels)
df2$c<-factor(df1$c,clevels,clabels)
I am using R to create frequency tables for survey responses and use value labels to create more useful frequency tabels.
I also edit the data in R before the frequency tables are created to remove invalid values and use write.csv to export the data.frame for reference.
I want the exported data.frame to show the coding for each question rather than it's label. Currently to do this I create a new labelled data.frame for the frequencies and export the original data.frame.
This leads to the danger of differences between the reference and analysis data.frames.
Is there a way of exporting the levels rather than the labels to a csv file?
Create a function to strip the levels from a single vector:
f <- function(x) {
if(is.factor(x)) {
return(as.numeric(x))
} else {
return(x)
}
}
Apply that function to the data.frame:
df <- sapply(f,df)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.