Hi Georg, You may find the "add.value.labels" function in the prettyR package useful. Jim On Tue, May 31, 2016 at 10:00 PM, <G.Maubach at weinwolf.de> wrote:> Hi All, > > I am using R for social sciences. In this field I am used to use short > variable names like "q1" for question 1, "q2" for question 2 and so on and > label the variables like q1 : "Please tell us your age" or q2 : "Could you > state us your household income?" or something similar indicating which > question is stored in the variable. > > Similar I am used to label values like 1: "Less than 18 years", 2 : "18 to > 30 years", 3 : "31 to 60 years" and 4 : "61 years and more". > > I know that the packages Hmisc and memisc have a functionality for this > but these labeling functions are limited to the packages they were defined > for. Using the question tests as variable names is possible but very > inconvenient. > > I there another way for labeling variables and values in R? > > Kind regards > > Georg Maubach > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
G.Maubach at weinwolf.de
2016-Jun-01 17:37 UTC
[R] Antwort: Re: Variable labels and value labels
Hi Jim, many thanks for the hint. When looking at the documentation I did not get how I do control which value gets which label. Is it possible to define it? Kind regards Georg Von: Jim Lemon <drjimlemon at gmail.com> An: G.Maubach at weinwolf.de, r-help mailing list <r-help at r-project.org>, Datum: 01.06.2016 03:59 Betreff: Re: [R] Variable labels and value labels Hi Georg, You may find the "add.value.labels" function in the prettyR package useful. Jim On Tue, May 31, 2016 at 10:00 PM, <G.Maubach at weinwolf.de> wrote:> Hi All, > > I am using R for social sciences. In this field I am used to use short > variable names like "q1" for question 1, "q2" for question 2 and so onand> label the variables like q1 : "Please tell us your age" or q2 : "Couldyou> state us your household income?" or something similar indicating which > question is stored in the variable. > > Similar I am used to label values like 1: "Less than 18 years", 2 : "18to> 30 years", 3 : "31 to 60 years" and 4 : "61 years and more". > > I know that the packages Hmisc and memisc have a functionality for this > but these labeling functions are limited to the packages they weredefined> for. Using the question tests as variable names is possible but very > inconvenient. > > I there another way for labeling variables and values in R? > > Kind regards > > Georg Maubach > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Hi Georg, add.value.labels simply creates an attribute named "value.labels" for the sorted values of the vector passed to it. The value labels passed become the names of this attribute in the sorted order. The function is intended to mimic a factor in reverse. While the factor adds sequential numeric values to the original values, add.value.labels adds names to the values passed. It was intended to be a mnemonic for numeric values that perhaps should have been coded as character. If I wrote this function now, it would probably look like this: value.labels<-function(x,labels) { if(missing(labels)) return(attr(x,"value.labels")) else { attr(x,"value.labels") <- sort(unique(x)) lenvallab <- length(attr(x,"value.labels")) if (length(labels) > lenvallab) { cat("More value labels than values, only the first", lenvallab, "will be used\n") labels <- labels[1:lenvallab] } names(attr(x, "value.labels"))<-labels return(x) } } age<-sample(1:5,100,TRUE) value.labels(age) age<-value.labels(age,c("0-19","20-39","40-59","60-79","80+")) age value.labels(age) Jim On Thu, Jun 2, 2016 at 3:37 AM, <G.Maubach at weinwolf.de> wrote:> Hi Jim, > > many thanks for the hint. > > When looking at the documentation I did not get how I do control which > value gets which label. Is it possible to define it? > > Kind regards > > Georg > > > > > Von: Jim Lemon <drjimlemon at gmail.com> > An: G.Maubach at weinwolf.de, r-help mailing list <r-help at r-project.org>, > > Datum: 01.06.2016 03:59 > Betreff: Re: [R] Variable labels and value labels > > > > Hi Georg, > You may find the "add.value.labels" function in the prettyR package > useful. > > Jim > > On Tue, May 31, 2016 at 10:00 PM, <G.Maubach at weinwolf.de> wrote: >> Hi All, >> >> I am using R for social sciences. In this field I am used to use short >> variable names like "q1" for question 1, "q2" for question 2 and so on > and >> label the variables like q1 : "Please tell us your age" or q2 : "Could > you >> state us your household income?" or something similar indicating which >> question is stored in the variable. >> >> Similar I am used to label values like 1: "Less than 18 years", 2 : "18 > to >> 30 years", 3 : "31 to 60 years" and 4 : "61 years and more". >> >> I know that the packages Hmisc and memisc have a functionality for this >> but these labeling functions are limited to the packages they were > defined >> for. Using the question tests as variable names is possible but very >> inconvenient. >> >> I there another way for labeling variables and values in R? >> >> Kind regards >> >> Georg Maubach >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. > >