- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一大堆时间间隔不规则的数据帧。
我想创建一个新的 data.frame 并将其他 data.frame 加入其中,对于加入的每个 data.frame,从新的 data.frame 中选取最新的值。
例如,下面的 listOfDataFrames 包含一个 data.frames 列表,每个 data.frames 都有一个以秒为单位的时间列。我找到总范围,将范围除以 60,然后对其进行排序,以获得完整分钟的递增 seqn。现在我需要将 data.frames 列表合并到这个新 seqn 的左侧。例如如果 mypoints 中的值为 60,则加入的值应为最新值 <= 60。
xrange <- range(lapply(listOfDataFrames,function(x) range(x$Time)))
mypoints <- 60*do.call(seq,as.list(xrange%/%60))
我相信这有时称为 asof 连接。
有一个简单的程序可以做到这一点吗?
谢谢
编辑:这是我目前使用的
xrange <- range(lapply(listOfDataFrames,function(x) range(x$Time)))
mypoints <- 60*seq(xrange[1]%/%60,1+xrange[2]%/%60)
result <- data.frame(Time=mypoints)
for(index in 1:length(listOfDataFrames))
{
x<-listOfDataFrames[[index]]
indices <- which(sort(c(mypoints,x$Time)) %in% mypoints) - 1:length(mypoints)
indices[indices==0] <- NA
newdf<-data.frame(new=x$Result[indices])
colnames(newdf)<-paste("S",index,sep="")
result <- cbind(result,newdf)
}
编辑:完整示例
AsOfJoin <- function (listOfDataFrames) {
xrange <- range(lapply(listOfDataFrames,function(x) range(x$Time)))
mypoints <- 60*seq(xrange[1]%/%60,1+xrange[2]%/%60)
result <- data.frame(Time=mypoints)
for(index in 1:length(listOfDataFrames))
{
x<-listOfDataFrames[[index]]
indices <- which(sort(c(mypoints,x$Time)) %in% mypoints) - 1:length(mypoints)
indices[indices==0] <- NA
newdf<-data.frame(new=x$Result[indices])
colnames(newdf)<-paste("S",index,sep="")
result <- cbind(result,newdf)
}
result[is.na(result)]<-0
result
}
a<-data.frame(Time=c(28947.5,28949.6,29000),Result=c(10,15,9))
b<-data.frame(Time=c(28947.8,28949.5),Result=c(14,19))
listOfDataFrames <- list(a,b)
result<-AsOfJoin(listOfDataFrames)
> a
Time Result
1 28947.5 10
2 28949.6 15
3 29000.0 9
> b
Time Result
1 28947.8 14
2 28949.5 19
> result
Time S1 S2
1 28920 0 0
2 28980 15 19
3 29040 9 19
最佳答案
data.table提供非常快速的开箱即用的 asof
连接。另请参阅This post举个例子
关于R 合并 data.frames asof join,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6585820/
我有两个 pandas 数据框 X 和 Y,每个都包含过去一个月的日内价格和时间数据。我想在 X 上运行 Y 的连接,即每次我们看到 X 的更新时我们都采用 Y 的现行价格。我想运行日内分析(因为隔夜
我已经阅读了 Pandas 的文档。有一个有用的函数叫做 merge_asof这似乎将两个数据帧与靠近的行合并在一起。但我不知道是什么asof方法。是吗as of ?或者它是某种东西的缩写? http
我希望做一些类似 Pandas 的事情 merge_asof 或 QuestDB 的 ASOF JOIN 在 Julia 。 至关重要的是,我还需要应用分组操作 . 我很乐意使用 Julia 的任何
我有每个实体的时间序列数据: id event_date value 1 2013-12-21 3.82 1 2013-12-22 2.47 1 2013-12-25 2.13
我有一大堆时间间隔不规则的数据帧。 我想创建一个新的 data.frame 并将其他 data.frame 加入其中,对于加入的每个 data.frame,从新的 data.frame 中选取最新的值
我有两个数据框,我希望将它们连接在一起,其中左侧数据框的信息索引为(日期,ID),右侧数据框的信息索引为(期间,ID),其中期间为年月。 我最终对左帧按 ID 进行分组,迭代组,在右帧上选择相同的组,
aj[`time`sym;trade;quote] 将每笔交易与之前的报值(value)连接起来。 我想进行相同的连接,但是是在下一个引用值而不是前一个值上进行连接。 我怎样才能实现这个目标? 最佳答
我见过这个问题被问过几次,但没有答案。简短版本: 我有一个带有两级MultiIndex索引的pandas DataFrame;两个级别都是整数。如何在此 DataFrame 上使用 .asof()?
kdb+ 有一个 aj通常用于沿时间列连接表的函数。 这是一个例子,我有交易和报价表,我得到了每笔交易的现行报价。 q)5# t time sym price size ------
我对excel一无所知,我想知道是否有一种方法可以将q-coding中的asof join语句转换为使用excel公式的语句 :update string issueSeries from aj[`s
我在传递 datetime 时遇到问题进入Pandas.Series.asof : def valueAsOf(self, date): if type(date) is str:
如何使用Series.asof函数?我传入了一个 datetime 对象 datetime.strptime('20150101', '%Y%m%d'),但为什么会报错 File "/Users/
我是一名优秀的程序员,十分优秀!