# R code for subsetting
library(Sleuth3)
#' ## Logical statement examples
x <- 1:5
y <- c("a","a","b","b","c")
x == 2
x > 2
x != 2
# y is equal to the character "a", element by element comparison
y == "a"
# this gives an error because R interprets the right hand side as the object, a,
# but no object called a exists.
y == a
a <- "b"
# how does this work?
y == a
# y is equal to a and x is equal to 2
y == "a" & x == 2
# TRUE if an element in y is one of "b" or "c"
y %in% c("b","c")
# returns the elements of y, that correspond to the elements of x equal to 2.
subset(y, x == 2)
#' ## Subsetting the pets data.frame
# setting up the data.frame - you don't need to know this,
# but you need it to create the pets data.frame
pets <- data.frame(pet_name = c("Dexter", "Scylla", "Medusa", "Mina"),
type = c("cat","cat","cat","dog"),
owner_name = c(rep("Charlotte", 2),
rep("Josh", 1), rep("Hadley", 1)),
age = c(3, 2, 11, 5))
# Answers
subset(pets, owner_name == "Charlotte")
subset(pets, owner_name != "Charlotte")
subset(pets, type == "cat")
subset(pets, type == "cat" & age > 2)
# a data.frame with no rows.
subset(pets, type == "dog" & age < 4)
# Find the mean age of Charlotte's pets
mean(subset(pets, owner_name == "Charlotte")$age)
# or assign the subsetted data.frame first
my_pets <- subset(pets, owner_name == "Charlotte")
# then find the mean age, or other summaries
mean(my_pets$age)
sd(my_pets$age)
length(my_pets$age)
# Find the mean age of pets that aren't Charlotte's
mean(subset(pets, owner_name != "Charlotte")$age)