创建群体平均值的变量
我正在与一个大型调查数据集合作,该数据集由几个国家和年份组成。我正在尝试创建一个新变量,这是特定年份中每个国家的平均信任得分。我想创建一个线图,以显示多年来每个国家的信任模式。我有一个变量,即“国家年份”,该变量决定了国家和调查年。 当我使用下面的代码时,我只会得到一个具有所有信任分数的总均值的变量,而不是特定的country_ year均值信任分数。
data<-data%>%
group_by(country_year)%>%
mutate(averagetrust = mean(trust))
My dataset looks something like this, but with 31 countries and 342 country/year combinations. The trust scores are individual trust scores for each respondents
# country year country_year trust
# 1 Austria 2002 AT2002 4
# 2 Austria 2002 AT2002 9
# 55 Belgium 2002 BE2002 7
# 56 Belgium 2002 BE2002 3
# 91 Austria 2005 AT2005 2
# 91 Austria 2005 AT2005 6
# 141 Belgium 2005 BE2005 5
# 142 Belgium 2005 BE2005 9
I am working with a large survey dataset, comprised of several countries and years. I am trying to create a new variable that is the mean trust score of each country in a particular year. I want to create a line graph showing the patterns of trust for each country across the years. I have a variable which is 'country years' which determines the country and the year of the survey.
When I use the code below, I just get a variable which has the overall mean of all trust scores, rather than specific country_year mean trust scores.
data<-data%>%
group_by(country_year)%>%
mutate(averagetrust = mean(trust))
My dataset looks something like this, but with 31 countries and 342 country/year combinations. The trust scores are individual trust scores for each respondents
# country year country_year trust
# 1 Austria 2002 AT2002 4
# 2 Austria 2002 AT2002 9
# 55 Belgium 2002 BE2002 7
# 56 Belgium 2002 BE2002 3
# 91 Austria 2005 AT2005 2
# 91 Austria 2005 AT2005 6
# 141 Belgium 2005 BE2005 5
# 142 Belgium 2005 BE2005 9
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
总结
尝试一下Try it with
summarise