[英]R: how to use the aggregate()-function to sum data from one column if another column has a distinct value?
嘿,我对聚合功能有疑问。 我的数据如下所示:
transect_id year day month LST precipitation
1 TR001 2010 191 4 30.62083 0.0000
2 TR001 2010 191 4 30.62083 0.0003
3 TR001 2010 191 5 30.62083 0.0001
4 TR001 2010 191 7 30.62083 0.0000
5 TR001 2010 191 7 30.62083 0.0000
6 TR001 2011 191 7 30.62083 0.0007
我想对每年每个夸脱的降水量求和。 这意味着:每年总计1-3个月,4-6个月,7-9个月和10-12个月的降水量(以我为例,2010-2013年)。 并为其添加一列。 我认为我应该使用plyr包中的mutate()函数,然后执行类似的操作
weather_gam.mutated<-mutate(weather_gam, precipitation.spring=aggregate(precipitation by = list(Category=year)))
但是几个月来该怎么办? 我根本无法弄清楚。 我尝试了by = list(Category= month==1)
东西by = list(Category= month==1)
但是显然这不是在这里成功的必要条件。 因此,基本上我只是尝试做SUMIFS(F1:Fx, B1:Bx = "2010", D1:Dx = "1", D1:Dx = "2", D1:Dx = "3"
我只是希望通过设置
by = list(Category=year)
当年份相同时,它将自动总和,因此我不需要每年手动进行。 如果您对解决方法有完全不同的想法,我将非常感谢您的帮助。
这是dplyr
和lubridate
的解决方案; 这个想法是使用lubridate
的quarter
函数来找出属于哪个四分之一月。 创建“ Quarter
列,按“季度”分组,然后为每个组创建“总和”或“ precipitation
”。
library(lubridate)
library(dplyr)
df$month <- month(df$month)
df %>% mutate(Quarter = quarter(month)) %>% group_by(Quarter) %>% mutate(SumPre = sum(precipitation))
Source: local data frame [6 x 8]
Groups: Quarter
transect_id year day month LST precipitation Quarter SumPre
1 TR001 2010 191 4 30.62083 0e+00 2 4e-04
2 TR001 2010 191 4 30.62083 3e-04 2 4e-04
3 TR001 2010 191 5 30.62083 1e-04 2 4e-04
4 TR001 2010 191 7 30.62083 0e+00 3 7e-04
5 TR001 2010 191 7 30.62083 0e+00 3 7e-04
6 TR001 2011 191 7 30.62083 7e-04 3 7e-04
这里是aggregate
另一种方法
library(lubridate)
df$month <- month(df$month)
df$Quarter <- quarter(df$month)
aggregate(precipitation ~ Quarter, data = df, sum)
Quarter precipitation
1 2 4e-04
2 3 7e-04
df <- structure(list(transect_id = structure(c(1L, 1L, 1L, 1L, 1L,
1L), .Label = "TR001", class = "factor"), year = c(2010L, 2010L,
2010L, 2010L, 2010L, 2011L), day = c(191L, 191L, 191L, 191L,
191L, 191L), month = c(4L, 4L, 5L, 7L, 7L, 7L), LST = c(30.62083,
30.62083, 30.62083, 30.62083, 30.62083, 30.62083), precipitation = c(0,
3e-04, 1e-04, 0, 0, 7e-04)), .Names = c("transect_id", "year",
"day", "month", "LST", "precipitation"), row.names = c("1", "2",
"3", "4", "5", "6"), class = "data.frame")
使用dplyr而不是plyr:
library(dplyr)
d.in %>%
mutate(q=cut(month, c(0,3,6,9,12), labels=c("q1", "q2", "q3", "q4"))) %>%
group_by(year, q) %>%
mutate(sum.prec = sum(precipitation))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.