# Logical subsetting # Remember birth data library(SMPracticals) data(births) # say I want times just for day 2 births$day # which are day 2 births$day == 2 # data.frame rows for day 2 births[births$day == 2, ] # just the times for day 2 births$time[births$day == 2] # or births[births$day == 2, "time"] # equivalently subset(births, day == 2) subset(births, day == 2)$time # more complicated expressions # day 2 and labour longer than 8 hours subset(births, day == 2 & time > 8) # labour less than four hours or more than 8 subset(births, time < 4 | time > 8) # if you want to do something with the subset you need to save it to a variable short_long <- subset(births, time < 4 | time > 8) hist(short_long$time) # iris data head(iris) summary(iris) # Q: try to get Petal.Width for obs with Petal.Length > 3 # Q: of those that have Petal.Length > 3 how many are virginica # Q: how could I get every 2nd row? hint: remember recycling