【发布时间】:2019-08-28 23:41:02
【问题描述】:
我有一些数据,其中我对同一事件进行了多次观察。基于时间阈值,我想浓缩观察结果。但我想知道我正在浓缩多少(即有多少观察成为一个)。我不确定如何以这种方式遍历我的数据框。
我尝试过编写 for 循环、if 语句、while 语句,并且在 google 和堆栈溢出上不知疲倦地搜索。似乎没有什么与我需要做的事情有关。
这是我的数据的一个子集:
structure(list(date.time = structure(c(1465877617, 1465877774,
1465877816, 1465877844, 1465912214, 1465912806, 1465912862, 1465914033
), class = c("POSIXct", "POSIXt"), tzone = "America/New_York"),
time = structure(1:8, .Label = c("00:13:37", "00:16:14",
"00:16:56", "00:17:24", "09:50:14", "10:00:06", "10:01:02",
"10:20:33"), class = "factor"), X = c(1, 1, 1, 1, 1, 1, 1,
1), diff_time1 = structure(c(157, 42, 28, 34370, 592, 56,
1171, 2820), class = "difftime", units = "secs"), diff_time2 = c(FALSE,
FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, TRUE), new = c("start",
"include", "include", "end", "start", "include", "end", "start-end"
)), row.names = c(NA, 8L), class = "data.frame")
我们的目标是让它看起来像下面,但每个“模糊”的观察都有一个额外的样本大小列:
structure(list(n = 1:8, end = structure(c(1465877844, 1465912862,
1465914033, 1465916853, 1465921999, 1465928992, 1465933159, 1465937668
), class = c("POSIXct", "POSIXt")), start = structure(c(1465877617,
1465912214, 1465914033, 1465916853, 1465921999, 1465928647, 1465932867,
1465937418), class = c("POSIXct", "POSIXt")), date = structure(c(16966,
16966, 16966, 16966, 16966, 16966, 16966, 16966), class = "Date")), row.names = c(NA,
-8L), class = c("tbl_df", "tbl", "data.frame"))
【问题讨论】:
标签: r loops data-manipulation