Chapter 4 | Exercise 3
Create a factor subj1
, a copy of the factor subj
from the data set sent
, and conflate the levels “Animal” and “Hum” into one category “Animate”. Next, reorder the three new levels as follows: “Animate”, “MatObj” and “Abstr”.
First, load the package and the data, if you have not done so already:
> library(Rling)
> data(sent)
Create a copy of the factor:
> subj1 <- sent$subj
Next, add the new level “Animate”:
> subj1 <- factor(subj1, levels = c(levels(subj1), "Animate"))
Replace the values “Animal” and “Hum” with one value “Animate”:
> subj1[subj1=="Animal"|subj1=="Hum"] <- "Animate"
Get rid of the non-occurring levels:
> subj1 <- factor(subj1)
> levels(subj1)
[1] "Abstr" "Animate" "MatObj"
Change the order of factor levels:
> subj1 <- factor(subj1, levels = c("Animate", "MatObj", "Abstr"))
> levels(subj1)
[1] "Animate" "MatObj" "Abstr"