我一直无法在Stack Overflow上找到我的查询解决方案 . This post is similar,但我的数据集略有 - 而且重要的是 - 不同(因为我在我的分组变量中有多个'time'的度量) .
随着时间的推移,我对不同地点的生物进行了观察 . 这些网站进一步聚合到更大的区域,所以我想最终有一个我可以在ddply中调用的函数来汇总地理区域内每个时间段的数据集 . 但是,我无法获得我需要的功能 .
Question
如何循环通过时间段并与之前的时间段进行比较,计算交叉点(即两个时间段内发生的“站点”数量)和每个时间段内出现的数量之和?
Toy dataset:
time = c(1,1,1,1,2,2,2,3,3,3,3,3)
site = c("A","B","C","D","A","B","C","A","B","C","D","E")
df <- as.data.frame(cbind(time,site))
df$time = as.numeric(df$time)
My function
dist2 <- function(df){
for(i in unique(df$time))
{
intersection <- length(which(df[df$time==i,"site"] %in% df[df$time==i- 1,"site"]))
both <- length(unique(df[df$time==i,"site"])) + length(unique(df[df$time==i-1,"site"]))
}
return(as.data.frame(cbind(time,intersection,both)))
}
dist2(df)
What I get:
dist2(df)
时间交叉点
1 1 3 8
2 1 3 8
3 1 3 8
4 1 3 8
5 2 3 8
6 2 3 8
7 2 3 8
8 3 3 8
9 3 3 8
10 3 3 8
11 3 3 8
12 3 3 8
What I expect (hoped!) to achieve:
time intersection both
1 1 NA 4
2 2 3 7
3 3 3 8
一旦我有了一个工作函数,我想在整个数据集上使用ddply来计算每个区域的这些值 .
非常感谢任何指示,提示,建议!
我在跑步:
R version 3.1.2 (2014-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
3 回答
您可以使用
table
函数确定每个站点每次出现的次数:通过一些简单的操作,您可以构建相同大小的表,其中包含站点在上一个时间段内出现的次数:
确定与前一次迭代相同的站点数量或前一次迭代中唯一站点的数量加上当前迭代中唯一站点的数量现在是简单的矢量化操作:
因为这不涉及制作涉及成对时间值的一堆
intersection
或unique
调用,所以它应该比循环解决方案更有效:您可以修改该功能
这是我的记忆密集型提案
Output:
将0更改为NA,您就完成了 .