12.4 Subset Data Revisited
As suggestion in Section 10.4, you might be interested in only having certain observations in a new data set.
Using the state data, we could see which states satisfy both of the following conditions:
- Illiteracy is less than 1.2; and
- Murder is greater than 7.5
<- dat[which(dat$Illiteracy < 1.2 & dat$Murder > 7.5), ]
dat.subset dat.subset
You can check your results to see that all of the observations meet the appropriate criteria.
If instead of an AND criteria, we had a OR criteria such as: - South region; or - Murder is less than 7
<- dat[which(dat$state.region == "South" | dat$Murder < 7), ]
dat.subset dat.subset
Again, you can check your results to see that at least one of the criteria are satisfied. Where the murder rate is not less than 5, then the region must be south. You can verify that all southern states are included plus those whose murder rates are less than 5. Do you see any where both conditions are satisfied? (The answer is yes, e.g. Delaware.)