So I made a data frame of people's names, ages, and their favorite movies. I want to write a program that acts on the data frame to give me the average age of each person with a specific favorite movie. Here's what I have.
persons <- list(firstName = c("Steve","Bob","Bill","Chris","Matt","Evan"), lastName = c("Williams","Barker","Barker","Williams","Stevenson","Parker"), age = c(22,30,41,14,9,93), favoriteMovie = c("Alien","The Shining","The Shining","Halloween","Alien","Alien"))
d1 <- data.frame(persons$firstName,persons$lastName,persons$age,persons$favoriteMovie)
d1
persons.firstName persons.lastName persons.age persons.favoriteMovie
1 Steve Williams 22 Alien
2 Bob Barker 30 The Shining
3 Bill Barker 41 The Shining
4 Chris Williams 14 Halloween
5 Matt Stevenson 9 Alien
6 Evan Parker 93 Alien
So I can do it with a loop of if statements but I don't think this is the most efficient way to do this. I'm sure there's some sort of way to kind of single out values but I'm really not sure.