r - 来自两个不同表的值的总和
问题描述
我有一个关于从具有相同列名的不同表中添加行的问题。我有两个表的时间序列,其值为 8760 行(全年)。
表格1
Name Year Month Day Hour Value
Plant_1 2020 1 1 1 10
Plant_2 2020 1 1 1 20
Plant_3 2020 1 1 1 30
Plant_1 2020 1 1 2 40
Plant_2 2020 1 1 2 50
Plant_3 2020 1 1 2 60
表2
Name Year Month Day Hour Value
Plant_x 2020 1 1 1 1
Plant_y 2020 1 1 1 2
Plant_z 2020 1 1 1 3
Plant_x 2020 1 1 2 4
Plant_y 2020 1 1 2 5
Plant_z 2020 1 1 2 6
我想要的是,所有植物在同一时期的价值总和,比如
Year Month Day Hour Value
2020 1 1 1 66
2020 1 1 2 165
我不在乎植物的名称,但需要在一年中的每个小时获得总价值的总和。我试图做这样的事情,但不适用于两个以上的桌子,我有 9 到 10 个这样的桌子。任何人都可以帮助我改进此代码或我可以使用的任何其他功能吗?
SumOfValue <- Table1%>%
full_join(Table2) %>%
group_by (Year,Month,Day,Hour) %>%
summarise(Value=sum(Value))
任何帮助,将不胜感激。谢谢你。
解决方案
看起来您的两个数据框具有相同的确切格式,因此您可以只使用它们,然后rbind
获取每个Year
、Month
和.Day
Hour
df = rbind(a,b)%>%group_by(Year,Month,Day,Hour)%>%summarise(Value=sum(Value))
# Alternative as suggested by Sotos
bind_rows(a, b) %>%group_by(Year,Month,Day,Hour)%>%summarise(Value=sum(Value))
# A tibble: 2 x 5
# Groups: Year, Month, Day [?]
Year Month Day Hour Value
<int> <int> <int> <int> <int>
1 2020 1 1 1 66
2 2020 1 1 2 165
数据
a = structure(list(Name = structure(c(1L, 2L, 3L, 1L, 2L, 3L), .Label = c("Plant_1",
"Plant_2", "Plant_3"), class = "factor"), Year = c(2020L, 2020L,
2020L, 2020L, 2020L, 2020L), Month = c(1L, 1L, 1L, 1L, 1L, 1L
), Day = c(1L, 1L, 1L, 1L, 1L, 1L), Hour = c(1L, 1L, 1L, 2L,
2L, 2L), Value = c(10L, 20L, 30L, 40L, 50L, 60L)), class = "data.frame", row.names = c(NA,
-6L))
b = structure(list(Name = structure(c(1L, 2L, 3L, 1L, 2L, 3L), .Label = c("Plant_x",
"Plant_y", "Plant_z"), class = "factor"), Year = c(2020L, 2020L,
2020L, 2020L, 2020L, 2020L), Month = c(1L, 1L, 1L, 1L, 1L, 1L
), Day = c(1L, 1L, 1L, 1L, 1L, 1L), Hour = c(1L, 1L, 1L, 2L,
2L, 2L), Value = 1:6), class = "data.frame", row.names = c(NA,
-6L))