I am a beginner and I am trying to calculate the mean age of people going to each movie using a list() and dataframes. I don't know how to go about solving this problem any help would be greatly appreciated, Thanks.
This is what I have so far:
fname <- c("Jake", "Anthony", "Albert", "James", "Tony")
lname <- c("Copi", "Copi", "Einstein", "Gleick", "Gleick")
age <- c(33, 40, 77, 44, 50)
movie <- c("Iron Man", "Thor", "Iron Man", "Iron Man", "Thor")
df <- data.frame(fname, lname, age, movie, stringsAsFactors = FALSE)
my.list <- list(fname, lname, age, movie)
my.list[3]
cat("\n**** Mean age ****\n")
# Calculate the mean age for people in the same movie
mean <- mean(my.list[[3]])
print(mean)
cat("\n**** People in the family by last name ****\n")
# Calculate the number of people in family with the same last name
table <- table(my.list[2])
print(table)
Output:
**** Mean age ****
[1] 48.8
**** People in the family by last name ****
Copi Einstein Gleick
2 1 2