Skip to content
Prev 361382 / 398506 Next

Variable labels and value labels

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:
Message-ID: <CA+8X3fUTXAa1j9G72aPtJS9QRnQ+RON8jQms0nYBsevRd4TkbA@mail.gmail.com>
In-Reply-To: <OF94AAE502.B31492A3-ONC1257FC5.0060B3F0-C1257FC5.0060D37E@lotus.hawesko.de>