英文:
Is there a way to summarize values grouped by years while keeping the index?
问题
以下是您要翻译的内容:
"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!"
英文:
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!
答案1
得分: 3
你可以使用 aggregate
:
aggregate(value ~ ., df1, sum)
# year index value
#1 2015 1 15
#2 2016 1 9
#3 2017 1 29
#4 2018 2 27
#5 2019 2 10
或者使用你的代码,在 group_by
中添加 index
:
library(dplyr)
df1 %>%
group_by(year, index) %>%
summarise(value = sum(value, na.rm = T))
## A tibble: 5 × 3
## Groups: year [5]
# year index value
# <dbl> <dbl> <dbl>
#1 2015 1 15
#2 2016 1 9
#3 2017 1 29
#4 2018 2 27
#5 2019 2 10
英文:
You can use aggregate
:
aggregate(value ~ ., df1, sum)
# year index value
#1 2015 1 15
#2 2016 1 9
#3 2017 1 29
#4 2018 2 27
#5 2019 2 10
Or using your code, adding index
in the group_by
.
library(dplyr)
df1 %>%
group_by(year, index) %>%
summarise(value = sum(value, na.rm = T))
## A tibble: 5 × 3
## Groups: year [5]
# year index value
# <dbl> <dbl> <dbl>
#1 2015 1 15
#2 2016 1 9
#3 2017 1 29
#4 2018 2 27
#5 2019 2 10
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论