【发布时间】:2021-01-06 19:49:04
【问题描述】:
我有一个 data.frame,它为跨不同模拟的每个 x 和 y 坐标提供事件发生的时间。我已经使用 dput() 附加了下表的头部。
head_data<-structure(list(x = c(987.353265152362, 570.817987386894, 1147.5681499552,
637.526076016409, 1439.13510253106, 1396.6452808061), y = c(1802.08232812874,
349.336242713164, 1789.49467712533, 361.611973188148, 1492.44148360367,
1459.91771610835), id = 1:6, `simulation 1` = c(1100, 600, 1200,
400, 900, 1000), `simulation 2` = c(1500, 1400, 1600, 1200, 1200,
1300), `simulation 3` = c(1200, 1100, 1200, 1000, 900, 900),
`simulation 4` = c(1300, 800, 1200, 900, 1100, 1100), `simulation 5` = c(1500,
1200, 1400, 1100, 1300, 1200), `simulation 6` = c(200, 1400,
100, 1100, 600, 600)), row.names = c(NA, 6L), class = "data.frame")
先将数据融合成长格式
data_long <- melt(head_data, id.vars = c('x', 'y', 'id'), value.name = 'time', variable.name = 'sim')
然后我对事件的时间进行排序
times <- sort(unique(data_long$time))
现在,我通过总结每个 sim 的每个时间间隔的事件,将这个事件的 data.frame 转换为总流行率。
data_clust_10 <- data_long %>% group_by(sim) %>%
do(data.frame(time=times, infected=sapply(times, function(x) sum(.$time <= x))))
然后我通过删除阈值后的所有事件来过滤流行数据,完整数据中有 1000 个 x 和 y 坐标,但我们每个 sim 只处理 6 个人,所以假设 2 个事件。
data_clust_10_cut<-filter(data_clust_10, infected < 2)
是否可以将此数据帧转换回 head_data 的原始格式?我可以使用 dcast() 吗?我认为行数会有所不同,所以它不起作用还是我错了?我想这样做是因为我将使用精炼的数据来估计方差系数。考虑到这一点,我实际上认为在原始表 head_data 中安排时间,然后消除超出我的流行阈值计数的事件数量可能是最好的解决方案,但我有兴趣了解 dcast()可以在这种情况下使用。我希望根据事件发生的时间对原始表中的事件序列进行排序,然后我想消除所有事件发生后发生的所有事件,而不考虑时间。
【问题讨论】: