【发布时间】:2016-04-19 08:16:05
【问题描述】:
我有一个数据框,其中包含从不同季节采集的样本。我想总结一下哪些站点有不同年份的春季(3-5 月)和秋季(9-11 月)的样本。例如,如果站点 A 有 2007 年春季的样本,则单元格显示为“真”。这是一个示例数据集:
Dates <- data.frame(c(as.Date("2007-9-1"),
rep(as.Date("2008-3-1"), times = 3) ,
rep(as.Date("2008-9-1"), times = 3)))
Sites <- as.data.frame(as.factor(c("SiteA",rep(c("SiteA","SiteB","SiteC"), 2))))
Values <- data.frame(matrix(sample(0:50, 3.5*2, replace=TRUE), ncol=1))
Dataframe <- cbind(Dates,Sites,Values)
colnames(Dataframe) <- c("date","site","value")
我已经设法根据这些函数在这个数据框中创建了因子“季节”。
Dataframe$Months <- as.numeric(format(Dataframe$date, '%m'))
Dataframe$Season <- cut(Dataframe$Months,
breaks = c(1, 2, 5, 8, 11, 12),
labels = c("Winter", "Spring", "Summer", "Autumn", "Winter"),
right = FALSE)
但我不确定从这里去哪里。这是输出的样子。
A <- rep("TRUE",times = 3)
B <- c("FALSE",rep("TRUE",times = 2))
C <- c("FALSE",rep("TRUE",times = 2))
Output <- as.data.frame(rbind(A,B,C))
colnames(Output) <- c("Autumn.07","Spring.07","Autumn.08")
【问题讨论】: