r - 按时间对数据帧进行子采样,保持第二个值的顶部

标签 r dataframe sample posixct

我有一个包含带小数秒的时间戳的数据框。每秒不止一行,我想过滤成一行。我想在每一秒的顶部或之前提取值。

这是一个数据示例:

 > head(sg1, 13)
                      time  count
1  2013-02-25 15:55:35.941      0
2  2013-02-25 15:55:36.042   8263
3  2013-02-25 15:55:36.144 147536
4  2013-02-25 15:55:36.243 165041
5  2013-02-25 15:55:36.342 126064
6  2013-02-25 15:55:36.441 100275
7  2013-02-25 15:55:36.542 101944
8  2013-02-25 15:55:36.647 108880
9  2013-02-25 15:55:36.742  86690
10 2013-02-25 15:55:36.842  74476
11 2013-02-25 15:55:36.941  76285
12 2013-02-25 15:55:37.042  79145
13 2013-02-25 15:55:37.141  84434

其中,我想选择第 1 行和第 11 行。

> dput(head(sg1, 13))
structure(list(time = structure(c(1361807735.942, 1361807736.042, 
1361807736.145, 1361807736.244, 1361807736.343, 1361807736.442, 
1361807736.542, 1361807736.647, 1361807736.742, 1361807736.842, 
1361807736.942, 1361807737.042, 1361807737.142), class = c("POSIXct", 
"POSIXt"), tzone = "GMT"), count = c(0L, 8263L, 147536L, 165041L, 
126064L, 100275L, 101944L, 108880L, 86690L, 74476L, 76285L, 79145L, 
84434L)), .Names = c("time", "count"), row.names = c(NA, 13L), class = "data.frame")

最佳答案

难的是你想要

the values at or immediately before the top of each second.

因此,将时间四舍五入并取最大的一个不太有效,因为如果第二个的顶部有一个,它就会被放入错误的组。这种方法可以正确处理这种情况。

library("lubridate")
library("plyr")
ddply(sg1, .(ceiling_date(time, unit="second")), function(DF) {
  DF[which.max(DF$time - ceiling_date(DF$time)),]
})[,-1]

给出

                 time count
1 2013-02-25 15:55:35     0
2 2013-02-25 15:55:36 76285
3 2013-02-25 15:55:37 84434

为了证明这适用于一秒,将一秒添加到数据集中。

sg2 <- rbind(sg1, 
structure(list(time=structure(1361807737, class=c("POSIXct", "POSIXt"), 
tzone="GMT"), count=c(34567L)), .Names = c("time", "count"), row.names=c(NA,1L),
class="data.frame"))
sg2 <- sg2[order(sg2$time),]

ddply(sg2, .(ceiling_date(time, unit="second")), function(DF) {
  DF[which.max(DF$time - ceiling_date(DF$time)),]
})[,-1]

现在返回“上一”秒的新行。

                 time count
1 2013-02-25 15:55:35     0
2 2013-02-25 15:55:37 34567
3 2013-02-25 15:55:37 84434

关于r - 按时间对数据帧进行子采样,保持第二个值的顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15349056/

相关文章:

r - 在 missForest 包中并行化进程

r - 如何将循环合并到我的数据子集中

python - 将 Pandas DataFrame 中的行追加到新的 DataFrame

r - 使用特定于组的样本大小从数据框中采样

从省略上三角部分的文件中读取对称矩阵

r - 获取行的索引,其元素在 R 中向量指示的所有列中都等于 x

R-Shiny 使用 Reactive renderUI 值

python - DataFrame 的给定列非零的行数

matlab - 如何从单元阵列中随机抽取样本?

r - 如何使用 R 中前几行的示例数据填充非相邻行?