Finding the mean of a column for specific rows

 **Genre             IMDB Score**

1 Action 7.9
2 Adventure 7.9
3 Fantasy 7.9
4 Sci-Fi 7.9
5 Action 7.1
6 Adventure 7.1
7 Fantasy 7.1
8 Action 6.8
9 Adventure 6.8
10 Thriller 6.8
11 Action 8.5
12 Thriller 8.5
13 Action 6.6
...

I have some data from a movie dataset as shown above with 2 columns - 'Genre' and 'IMDB Score'. There are 24 unique genres and 1,000s of rows, each with a different IMDB score attached to them. In the end, I want a dataframe with 24 rows, each row a genre and each genre attached with the mean IMDB score for that genre i.e.

 **Genre             IMDB Score**

1 Action mean IMDB score for 'action'
2 Adventure mean IMDB score for 'adventure'
3 Fantasy mean IMDB score for 'fantasy'
...

How would I go about coding this?

I'm really new at R, so am not at all sure how to do this.

Typically this is a great case for dplyr,
using the group_by/summarize workflow.

i.e
imdb_data %>%
group_by(genre) %>%
summarize(mean_score = mean(IMDB_Score)

1 Like

Here are two ways to do it using data I invented. The apparent difference in precision of the two answers is just due to how the results are printed. The stored values are the same.

DF <- data.frame(Genre = sample(c("Action", "Fantasy", "SciFi"), 15, replace = TRUE),
                  Score = runif(15, min = 1, max = 10))
DF
#>      Genre    Score
#> 1   Action 4.503736
#> 2  Fantasy 6.345093
#> 3    SciFi 6.757014
#> 4  Fantasy 7.395923
#> 5    SciFi 7.066291
#> 6  Fantasy 2.975232
#> 7    SciFi 7.547725
#> 8    SciFi 8.265795
#> 9    SciFi 2.871826
#> 10 Fantasy 2.454224
#> 11   SciFi 6.984788
#> 12  Action 7.609046
#> 13  Action 6.964582
#> 14  Action 3.312916
#> 15 Fantasy 3.489738

Stats <- aggregate(Score ~ Genre, data = DF, FUN = mean)
Stats
#>     Genre    Score
#> 1  Action 5.597570
#> 2 Fantasy 4.532042
#> 3   SciFi 6.582240

library(dplyr)

Stats2 <- DF %>% group_by(Genre) %>% summarize(AvgScore = mean(Score))
Stats2
#> # A tibble: 3 x 2
#>   Genre   AvgScore
#>   <fct>      <dbl>
#> 1 Action      5.60
#> 2 Fantasy     4.53
#> 3 SciFi       6.58

Created on 2019-11-19 by the reprex package (v0.2.1)

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.