Factor column-label

512 views Asked by At

I have a data frame with plenty of factor columns, and these columns have labels explaining what these column names mean ( these have come from reading SAS xport files and are very useful to understand the columns ). When I convert these factor columns to character columns the column-labels are gone, how do we retain them? Same issue in data tables too.

Here is an example to show the behavior:

 d1<-data.frame(a=c(1,1,1,2,2),b=c("x1","x1","x2","x2","x3"))
 d1$a<-factor(d1$a)
 label(d1[["a"]])<-"Identity variable"
 label(d1[["b"]])<-"Single letter variable"
 label(d1)
                 a                        b 
 "Identity variable" "Single letter variable"

 i<-sapply(d1,is.factor)
 d1[i]<-lapply(d1[i],as.character,stringsAsFactors=F)
 label(d1)
                 a                        b 
             ""      "Single letter variable" 

How can I retain the labels while converting the columns in both data frames and data tables?


I tried the following based on Michal's suggestion :

 `[.avector` <- function(x,i,...) {
      r <- NextMethod("[")
      mostattributes(r) <- attributes(x)
     r
   }


 d2 <- data.frame( lapply( d1, function(x) {
      structure( x, class = c("avector", class(x) ) )
    } ) )

   i<-sapply(d2,is.factor)
   d2[i]<-lapply(d2[i],as.character)
   label(d2)

                   a                        b 
                  "" "Single letter variable" 

Does labels needs to be handled differently from comments?

1

There are 1 answers

1
Michal On

The labels are attributes.

I found this question that may be useful about preserving attributes. How to delete a row from a data.frame without losing the attributes