Je suis un débutant et j'essaie de calculer l'âge moyen des personnes qui vont à chaque film en utilisant une liste () et des dataframes. Je ne sais pas comment résoudre ce problème. Toute aide serait grandement appréciée, merci.
Voici ce que j'ai jusqu'à présent:
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
3 Réponses :
Utilisez agrégat . Avec votre df:
unlisted_df <- data.frame(age=unlist(my.list[[3]]),movie=unlist(my.list[[4]]))
aggregate(age ~ movie, data=unlisted_df, mean)
movie age
1 Iron Man 51.33333
2 Thor 45.00000
Pour utiliser votre liste, je voudrais honnêtement la désinscrire ..
aggregate(age ~ movie, data=df, mean)
movie age
1 Iron Man 51.33333
2 Thor 45.00000
p >
Merci, mais j'obtiens une erreur. Pourriez-vous aider? `` `` Erreur dans get (as.character (FUN), mode = "function", envir = envir): l'objet 'FUN' of mode 'function' n'a pas été trouvé `` ``
Essayez rm (mean) , puis réessayez? Cela est probablement dû au fait que vous avez nommé une variable mean , qui est aussi le nom de la fonction. À l'avenir, essayez de lui donner un nom différent :)
Une approche un peu différente sans utiliser la liste et la fonction d'agrégation:
#Your Dataframe initialisation
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)
#Creating a list of unique movies within the dataframe
movie_list = unique(df$movie)
counter = 1
#A Dataframe to store Movie along with the mean actor age
mean_df = data.frame(movie = character(), average_age = numeric(), stringsAsFactors = FALSE)
#Iterate over the movies
for(movie in movie_list){
#Locate their index in the main dataframe
movie_index = df$movie == movie
#Calculate the mean of "age" column within the located indexes
average = mean(df$age[movie_index])
#Append the movie name and the corresponding avg. age to the 'mean_df'
mean_df[counter,] = list(movie, average)
#A variable to access the main dataframe, row by row
counter = counter+1
}
print(mean_df)
Vous pouvez également essayer mean dans un by , en donnant cette sortie:
df <- structure(list(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")), class = "data.frame", row.names = c(NA,
-5L))
en utilisant en plus avec et round.
round(with(df, by(age, movie, mean)), 1) # movie: Iron Man # [1] 51.3 # ----------------------------------------------------------------------------------- # movie: Thor # [1] 45
Données
by(df$age, df$movie, mean) # df$movie: Iron Man # [1] 51.33333 # ----------------------------------------------------------------------------------- # df$movie: Thor # [1] 45