You need to apply some data transformation before applying How to sum a variable by group
(您需要先进行一些数据转换,然后再应用如何按组对变量求和)
aggregate(Tornado~Date, transform(df, Date = format(as.Date(paste(Month,Year,"01"),
"%B %Y %d"), "%Y-%m")), sum)
# Date Tornado
#1 1998-01 4
#2 1998-02 2
#3 1998-03 5
#4 2010-01 3
data
(数据)
df <- structure(list(Month = structure(c(2L, 1L, 3L, 2L, 2L),
.Label = c("February", "January", "March"), class = "factor"),
Year = c(1998L, 1998L,1998L, 1998L, 2010L),
Tornado = c(3L, 2L, 5L, 1L, 3L), Location = structure(c(2L,
2L, 2L, 1L, 2L), .Label = c("Florida", "Illinois"), class = "factor")),
class = "data.frame", row.names = c(NA, -5L))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…