Richard M. Heiberger
2006-Jun-04 16:12 UTC
[R] Fwd: Re: How to call a value labels attribute?
How is what you are doing any different from factors?> x <- factor(c(1, 2, 3, 3, 2, 3, 1), labels=c("apple", "banana", "other")) > x[1] apple banana other other banana other apple Levels: apple banana other> as.numeric(x)[1] 1 2 3 3 2 3 1> levels(x)[3] <- "birne" > x[1] apple banana birne birne banana birne apple Levels: apple banana birne>---- Original message ---->### not run >### pseudocode >x <- c(1, 2, 3, 3, 2, 3, 1) >value.labels(x) <- c(apple=1, banana=2, NA=3) >x >### desired result >apple banana NA NA banana NA apple > 1 2 3 3 2 3 1 > >value.labels(x) <- c(Apfel=1, Banane=2, Birne=3) # redefine labels >x >### desired result >Apfel Banane Birne Birne Banane Birne Apfel > 1 2 3 3 2 3 1 > >value.labels(x) # inspect labels >### desired result >Apfel Banane Birne > 1 2 3
Richard, Martin, the example is not ideal, I see. I was strating from the question, how to represent a metric categorical variable. By "metric categorical variable" I intend a variable, which has only few distinct values and an inherent metric. An example would be a risk score, which classifies patients in several groups like "low, intermediate, high, extreme" with corresponding risk estimates of "0, 1, 2, 5.5". Other examples could be items in a questionnaire. These items often have numerical values that may range from -5 to 5. In some cases, like tables and box-plots these scores/items should be treated like a factor (with labelled values), in other cases like cox-regression or when forming an overall score they should be treated like numeric variables. I was asking for a convenient way to represent a variable like this, but there was no response. The crucial point is that the variables should retain their numerical values and their value labels. Without value labels they could be defined as factor and used or directly or by as.numeric(), because the levels still represent the numerical values, but as soon as labels are used, the original numerical values get lost. Thanks, Heinz T?chler At 12:12 04.06.2006 -0400, Richard M. Heiberger wrote:>How is what you are doing any different from factors? > > >> x <- factor(c(1, 2, 3, 3, 2, 3, 1), labels=c("apple", "banana", "other")) >> x >[1] apple banana other other banana other apple >Levels: apple banana other >> as.numeric(x) >[1] 1 2 3 3 2 3 1 >> levels(x)[3] <- "birne" >> x >[1] apple banana birne birne banana birne apple >Levels: apple banana birne >> > > >---- Original message ---- >>### not run >>### pseudocode >>x <- c(1, 2, 3, 3, 2, 3, 1) >>value.labels(x) <- c(apple=1, banana=2, NA=3) >>x >>### desired result >>apple banana NA NA banana NA apple >> 1 2 3 3 2 3 1 >> >>value.labels(x) <- c(Apfel=1, Banane=2, Birne=3) # redefine labels >>x >>### desired result >>Apfel Banane Birne Birne Banane Birne Apfel >> 1 2 3 3 2 3 1 >> >>value.labels(x) # inspect labels >>### desired result >>Apfel Banane Birne >> 1 2 3 > >