I have a dataset and a list of labels. I simply want to apply the labels to the variables, all at once. The only way I was able to do it was using a loop: for (i in 1:length(data)) label(data[,i]) <- data.labels[i] I'd like to find the non-loop way to do it, using apply or the like... Any help appreciated. [[alternative HTML version deleted]]
Sarah Goslee
2011-Aug-15 21:03 UTC
[R] Labelling all variables at once (using Hmisc label)
On Mon, Aug 15, 2011 at 3:53 PM, Monsieur Do <nonaupourriel at yahoo.ca> wrote:> I have a dataset and a list of labels. I simply want to apply the labels to the variables, all at once. The only way I was able to do it was using a loop: > > for (i in 1:length(data)) label(data[,i]) <- data.labels[i] > > I'd like to find the non-loop way to do it, using apply or the like... Any help appreciated.Would you not be better off with names()?> data <- 1:10 > data.labels <- letters[1:10] > names(data) <- data.labels > dataa b c d e f g h i j 1 2 3 4 5 6 7 8 9 10 What are you trying to do with label() that names() doesn't accomplish? -- Sarah Goslee http://www.functionaldiversity.org
The labels can contain much more than just names. In my case, they are variable descriptions (items from a questionnaire). I need to keep the names as they are, hence the need for Hmisc's labels. On Mon, Aug 15, 2011 at 3:53 PM, Monsieur Do <nonaupourriel@yahoo.ca> wrote:> I have a dataset and a list of labels. I simply wantto apply the labels to the variables, all at once. The only way I was able to do it was using a loop:> > for (i in1:length(data)) label(data[,i]) <- data.labels[i]> > I'd like to find the non-loop way to do it, usingapply or the like... Any help appreciated. Would you not be better off with names()?> data <- 1:10 > data.labels <- letters[1:10] > names(data) <- data.labels > dataa b c d e f g h i j 1 2 3 4 5 6 7 8 9 10 What are you trying to do with label() that names() doesn't accomplish? -- Sarah Goslee http://www.functionaldiversity.org [[alternative HTML version deleted]]
I did read the help page before posting, but didn't find the direct way... My function here works fine. But just for learning purposes, I'd like to be able to avoid the loop... with.labels <- function(x, labels=NULL, csvfile=NULL) { if(!is.null(csvfile)) labels <- read.csv(csvfile, sep="\t", header=F, stringsAsFactors=F)[,1] for(i in 1:length(x)) label(x[,i]) <- labels[i] if(length(labels) != length(x)) cat("Warning: data and labels are not of same length\n") return(x) } Thanks> Message: 11 > Date: Tue, 16 Aug 2011 04:22:07 -0700 (PDT) > From:Frank Harrell <f.harrell@vanderbilt.edu>> To:r-help@r-project.org> Subject:Re: [R] Labelling all variables at once (using Hmisc label)> Message-ID:<1313493727519-3746928.post@n4.nabble.com>> Content-Type:text/plain; charset=UTF-8> > Dorequire(Hmisc); ?label to see the help file for label. It will show you> how todo this:> > MonsieurDo wrote:> >I have a dataset and a list of labels. I simply want> > toapply the labels to the variables, all at once. The only way I was able> > to doit was using a loop:> > > > for (i in 1:length(data)) label(data[,i]) <-data.labels[i] > > > > I'd like to find the non-loop way to do it, using> >apply or the like... Any help appreciated.> > > -----> FrankHarrell> Departmentof Biostatistics, Vanderbilt University [[alternative HTML version deleted]]
Indeed, as David pointed out, all the portion that used courier font (all the good stuff) was absent from the email posting. Thanks for your answers. [[alternative HTML version deleted]]
Frank Harrell
2011-Aug-20 00:57 UTC
[R] Labelling all variables at once (using Hmisc label)
Sorry about the nabble problem. At any rate, do require(Hmisc) then ?label to see how to associate a vector of labels with all the variables in a data frame at once. Frank do999 wrote:> > Indeed, as David pointed out, all the portion that used courier font (all > the good stuff) was absent from the email posting.? > > > Thanks for your answers. > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help at 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. >----- Frank Harrell Department of Biostatistics, Vanderbilt University -- View this message in context: http://r.789695.n4.nabble.com/Labelling-all-variables-at-once-using-Hmisc-label-tp3745660p3756459.html Sent from the R help mailing list archive at Nabble.com.