Adrian DUSA wrote:> Dear all,
>
> In order to ease the transition from SPSS to R for some of my colleagues, I
am
> trying to create a function which would show the variables and their labels
> (if those exist), using function "label" in package Hmisc.
>
> A toy example would be this:
>
> my.data <- data.frame(age=c(24,35,28), gender=c("Male",
"Female", "Male"))
> require(Hmisc)
> label(my.data$age) <- "Respondent's age"
> label(my.data$gender) <- "Responent's gender"
>
> variables <- function(x) {
> dataf <- data.frame(variable=NA, label=NA)
> varlab <- NA
> for (i in 1:length(names(x))) {
> dataf[i,1] <- names(x)[i]
> dataf[i,2] <- label(x[,i])
> varlab[i] <- label(x[,i])
> }
> fix(dataf)
> # I assume this would return a modified dataf
> for (i in which(varlab != dataf[,2])) {
> label(x[,i]) <- dataf[i,2]
> }
> }
>
> Now, say during fix() one modified "Responent's gender" into
"Respondent's
> gender" (the previous missed a "d"). The trouble I'm
having is to return the
> modified object, with the modified labels. It should be easy, I feel it,
but
> I just can't get it.
>
> Thank you in advance,
> Adrian
>
Hi, Adrian,
You need to assign "fix(dataf)" to something:
my.data <- data.frame(age=c(24,35,28), gender=c("Male",
"Female", "Male"))
require(Hmisc)
label(my.data$age) <- "Respondent's age"
label(my.data$gender) <- "Responent's gender"
variables <- function(x) {
dataf <- data.frame(variable=NA, label=NA)
varlab <- NA
for (i in 1:length(names(x))) {
dataf[i,1] <- names(x)[i]
dataf[i,2] <- label(x[,i])
varlab[i] <- label(x[,i])
}
dataf <- fix(dataf)
# I assume this would return a modified dataf
for (i in which(varlab != dataf[,2])) {
label(x[,i]) <- dataf[i,2]
}
# don't forget to return dataf
dataf
}
variables(my.data)
HTH,
--sundar