Sometimes it is useful to split a vector or data frame into pieces according to the value of a variable. For example, from
m111survey we might like to have separate data frames for each of the three seating preferences. We can accomplish this with the
bySeat <- split(m111survey, f = m111survey$seat)
If you run the command
str(bySeat), you find that
bySeat is a list consisting of three data frames:
1_front: the frame of all subjects who prefer the Front;
2_middle: the frame of all subjects who prefer the Middle;
3_back: the frame of all subjects who prefer the Back.
Now you can carry on three separate analyses, working with one frame at a time.
There is a pitfall which of you should be aware. If you try to access any one of the frames by its name, you will get an error:
## Error: unexpected numeric constant in "bySeat$1"
The reason is that variable names cannot begin with a number! You have two options, here. You could access a single frame by using the name in quotes:
Your second option is to use the index of the element you want: