Change reference level for variable in R -
i have data set, (call data) variable, color. mode of color numeric , class factor. first, i'm bit confused "numeric" -- when printed out, data color not numeric -- character values, white or blue or black, etc. clarification on appreciated.
further, need write r code return levels of color variable, determine current reference level of variable, , set reference level of variable white. tried using factor, entirely unsuccessful.
thank taking time help.
mode(data$color)
"numeric"
because r internally stores factors numeric codes (to save space), plus associated vector of labels corresponding code values. when print factor, r automatically substitutes corresponding label each code.
f <- factor(c("orange","banana","apple")) ## [1] orange banana apple ## levels: apple banana orange str(f) ## factor w/ 3 levels "apple","banana",..: 3 2 1 c(f) ## strip attributes numeric vector ## [1] 3 2 1 attributes(f) ## $levels ## [1] "apple" "banana" "orange" ## $class ## [1] "factor"
... need write r code return levels of color variable ...
levels(data$color)
... determine current reference level of variable,
levels(data$color)[1]
... , set reference level of variable white.
data$color <- relevel(data$color,"white")