我不确定我是否理解您的问题。因此,我为您提供了一个非常基本的解决方案,并且我将所有阶段分开,因此一定有一些对您有用的地方;-)
告诉我进展如何!
# Let's first create some fake data
survival <- sample(1:2, 100, replace=T)
year <- sample(1940:1990, 100, replace=T)
comparison <- data.frame(year, survival)
# We sum each category for each year
sum.per.year <- rbind(tapply(comparison$survival, comparison$year, function(x) sum(x==1)),
tapply(comparison$survival, comparison$year, function(x) sum(x==2)))
# We create a logical vector to check what years are in the period we target
year.before.1965 <- sapply(colnames(sum.per.year), function(c) as.numeric(c)<1965)
# Now we aggregate (sum) the results for before and after 1965
sum.per.year <- t(data.matrix(sum.per.year))
res <- sapply(1:2,function(c) tapply(sum.per.year[,c],year.before.1965,sum))
colnames(res) <- c("Lived.over.5","Died.in.5")
rownames(res) <- c("Before.1965","After.1965")
# And finally we output
barplot(res, beside=T, col=c("blue","red"))
legend("topleft", rownames(res), pch=15, col=c("blue","red"), bty="o")
# OR, depending on what how you want to present the results
barplot(t(res), beside=T, col=c("blue","red"))
legend("topleft", colnames(res), pch=15, col=c("blue","red"), bty="o")