Professional Documents
Culture Documents
Statistics 22
Statistics 22
Statistics 22
Now imagine that you’ve used the numeric code to tabulate the eye colors of 14
people and then turned those codes into a vector:
> eye_color <- c(2,2,4,1,5,5,5,6,1,3,6,3,1,4)
Next, you use the factor() function to turn eye_color into a factor:
> feye_color <- factor(eye_color)
Finally, you assign the levels of the factor:
> levels(feye_color) <- c("amber","blue", "brown","gray","green",
"hazel")
Now, if you examine the eye color data in terms of the factor levels, it looks like
this:
> feye_color
[1] blue blue gray amber green green green hazel amber
[10] brown hazel brown amber gray
Levels: amber blue brown gray green hazel
Lists
In R, a list is a collection of objects that aren’t necessarily of the same type.
Sup-
pose that in addition to the eye color of each person in the example in the preced-
ing section, you collect an “empathy score” based on a personality test. The scale
runs from 0 (least empathy) to 100 (most empathy). Here’s the vector for these
people’s empathy data:
> empathy_score <- c(15,21,45,32,61,74,53,92,83,22,67,55,42,44)
You want to combine the eye color vector in coded form, the eye color vector in
factor form, and the empathy score vector into one collection named eyes_and_
empathy . You use the list() function for this task:
> eyes_and_empathy <- list(eyes_code=eye_color, eyes=feye_color,
empathy=empathy_score)
Note that you name each argument ( eyes_code , eyes , and empathy ). This causes
R to use those names as the names of the list components.