diff --git a/r.html.markdown b/r.html.markdown index 3e855602..79af40ce 100644 --- a/r.html.markdown +++ b/r.html.markdown @@ -255,16 +255,16 @@ c('Z', 'o', 'r', 'r', 'o') == "Z" # TRUE FALSE FALSE FALSE FALSE # FACTORS # The factor class is for categorical data -# Factors can be ordered (like childrens' grade levels) or unordered (like gender) -factor(c("female", "female", "male", NA, "female")) -# female female male female -# Levels: female male +# Factors can be ordered (like childrens' grade levels) or unordered (like colors) +factor(c("blue", "blue", "green", NA, "blue")) +# blue blue green blue +# Levels: blue green # The "levels" are the values the categorical data can take # Note that missing data does not enter the levels -levels(factor(c("male", "male", "female", NA, "female"))) # "female" "male" +levels(factor(c("green", "green", "blue", NA, "blue"))) # "blue" "green" # If a factor vector has length 1, its levels will have length 1, too -length(factor("male")) # 1 -length(levels(factor("male"))) # 1 +length(factor("green")) # 1 +length(levels(factor("green"))) # 1 # Factors are commonly seen in data frames, a data structure we will cover later data(infert) # "Infertility after Spontaneous and Induced Abortion" levels(infert$education) # "0-5yrs" "6-11yrs" "12+ yrs"