I tried to summarize values of different years which are assigned to specific IDs.
I used dplyr
to summarize it but did not find a way to keep the index.
My data
looks something like this:
year <- c(2015, 2015, 2015, 2016, 2016, 2017, 2017, 2018, 2018, 2018, 2018, 2019, 2019)
index <- c(1,1,1,1,1,1,1,2,2,2,2,2,2)
value <- c(5,7,3, NA,9,14, 15, 8, NA, 9, 10, 6, 4)
df1 <- data.frame(year, index, value)
And that is the way i summarized the data:
sum1 <-
df1 %>%
group_by(year) %>%
summarise(value = sum(value, na.rm = T))
I'd like to get an outcome like:
year1 <- c(2015, 2016, 2017, 2018, 2019)
index1 <- c(1, 1, 1, 2, 2)
value1 <- c(15, 9, 29, 27, 10)
df2 <- data.frame(year1, index1, value1)
Thanks, I really appreciate your help!