1

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
  • 3
    It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. While it is clear what you are trying to achieve your question lacks the necessary details to figure out what's the issue. – stefan Apr 17 '22 at 11:02
  • 1
    Thanks for the suggestion, I have edited my question to include a snippet of what my data looks like – user18325005 Apr 17 '22 at 11:39

1 Answers1

0

Try it with summarise

library(gapminder)
library(tidyverse)

gapminder%>%
  group_by(country, year)%>%
  filter(country %in% c("Germany", "Belgium", "France")) %>% 
  summarise(averagetrust = mean(pop, na.rm = T)) %>% 
  ggplot(aes(x = year, y = averagetrust, color = country)) + 
  geom_line()
Julian
  • 6,586
  • 2
  • 9
  • 33
  • When I do I just get the overall mean trust – user18325005 Apr 17 '22 at 14:25
  • `averagetrust 4.401746` – user18325005 Apr 17 '22 at 14:25
  • I edited it with an example from gapminder which is also a year-country panel. – Julian Apr 18 '22 at 08:27
  • I am able to calculate the means for each country year but I cannot attach them as a variable in my dataset, when I do I get this error message ```Error: ! Assigned data `value` must be compatible with existing data. x Existing data has 366816 rows. x Assigned data has 349 rows. i Only vectors of size 1 are recycled. Backtrace: 1. base::`$<-`(`*tmp*`, avgtrust, value = ``) 14. tibble ``(``)``` – user18325005 Apr 18 '22 at 15:18