I have my own function for doing this that is similar to the one presented 
below.  Others may have other ideas that work better.  As a general rule, I 
would caution against writing out just the label without the variable name.  
The only reason I see to separate the labels and names is if you are generating 
a report (and using write.table wouldn't be my first choice for doing that).  
However, the function below at least allows you to decide when you call it.  If 
you want to exclude the names, just set names=FALSE.


#*** possibly buggy.  I didn't do much testing on this function.
write.table.with.labels <- function(data, file, names=TRUE, labels=TRUE, 
append=FALSE, ...){
  if (names) write.table(t(names(data)), file, col.names=FALSE, ...)
  if (labels) write.table(t(label(data)), file, col.names=FALSE, 
append=any(c(names, append)), ...)
  write.table(data, file, col.names=FALSE, append=TRUE, any(c(names, labels, 
append)), ...)
}

label(mtcars$mpg) <- "Miles per gallon"

write.table.with.labels(mtcars, "test.csv", sep=",")



  Benjamin Nutter |  Biostatistician     |  Quantitative Health Sciences
  Cleveland Clinic    |  9500 Euclid Ave.  |  Cleveland, OH 44195  | (216) 
445-1365


-----Original Message-----
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Francois Maurice
Sent: Tuesday, July 17, 2012 4:36 PM
To: r-help@r-project.org
Subject: [R] Variable labels



Hi,
 
I'm using write.table() to export dataframe to Excel. All works fine except 
that I want to export the variable labels instead of variable names.

 I can see the labels in the R consol using attr(), but I just don't know how 
to use the labels instead of the names.

Thanks,

François Maurice
        [[alternative HTML version deleted]]


===================================


 Please consider the environment before printing this e-mail

Cleveland Clinic is ranked one of the top hospitals in America by U.S.News & 
World Report (2010).  
Visit us online at http://www.clevelandclinic.org for a complete listing of our 
services, staff and locations.


Confidentiality Note:  This message is intended for use ...{{dropped:18}}

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to