Re: [R] returning a modified fix()-ed dataframe

From: Sundar Dorai-Raj <sundar.dorai-raj_at_pdf.com>
Date: Sat 08 Oct 2005 - 03:55:38 EST

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



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Oct 08 04:12:46 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:40:39 EST