我不知道我是否理解你的问题。所以我给你提供一个非常基本的解决方案,我将所有阶段分开,以便在某处有一些有用的东西;-)
让我知道它是怎么回事!
# 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")
这正是我所需要的,非常感谢你!不是我期望的结果,但是这对我有很大帮助!再一次感谢你! :) – LunaRanger