我有一个关于来自不同国家的电影评级的数据集(列"电影","国家")及其在全球的评级(列#34;评级")。考虑到每个国家/地区有多部电影,我怎样才能找到每个国家/地区的评级均值?
答案 0 :(得分:1)
如果我得到数据格式,那么这样的东西应该可行。
require(data.table)
# create test data set, you can use setDT to create data.table from data.frame
set.seed(123)
dt <- data.table(movies=rep(c("Movie 1", "Movie 2", "Movie 3"), 3),
country=rep(c("US", "UK", "IE"), each=3), rating=sample(9))
# movies country rating
#1 Movie 1 US 3
#2 Movie 2 US 7
#3 Movie 3 US 9
#4 Movie 1 UK 6
#5 Movie 2 UK 5
#6 Movie 3 UK 1
#7 Movie 1 IE 2
#8 Movie 2 IE 8
#9 Movie 3 IE 4
# calculate average rating
dt[, .(rating = mean(rating)), by = country]