Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R merge data.asof join_R_Join_Dataframe - Fatal编程技术网

R merge data.asof join

R merge data.asof join,r,join,dataframe,R,Join,Dataframe,我有一大堆时间间隔不规则的数据帧 我想创建一个新的data.frame并将其他数据加入其中,对于每个要加入的data.frame,从新的data.frame中选取最新的值 例如,下面的listOfDataFrames包含一个data.frames列表,每个data.frames都有一个以秒为单位的时间列。我找到了总范围,将范围修改为60,并将其调整为,以获得全分钟的递增顺序。现在我需要将data.frames列表合并到这个新seqn的左侧。e、 g.如果mypoints中的值为60,则与之关联的

我有一大堆时间间隔不规则的数据帧

我想创建一个新的data.frame并将其他数据加入其中,对于每个要加入的data.frame,从新的data.frame中选取最新的值


例如,下面的listOfDataFrames包含一个data.frames列表,每个data.frames都有一个以秒为单位的时间列。我找到了总范围,将范围修改为60,并将其调整为,以获得全分钟的递增顺序。现在我需要将data.frames列表合并到这个新seqn的左侧。e、 g.如果mypoints中的值为60,则与之关联的值应为最新值有关答案,请参见我的编辑。显然是最好的方法。

提供非常快速的
asof
即时连接。
另请参见示例

您能否提供一个小示例,也许是一个2-3个数据帧的列表来说明您的问题?另外,如果没有唯一的最新值,您打算怎么办?列表应该全部排序。试想一下,候诊室里有多少病人,他们每一秒都随机到达。我们想知道每一分钟有多少人在等待,然后加入其中,例如5个工作日。通过让某人“想象”一些数据,你不会增加他们提供帮助的几率。帮助我们帮助你。我玩了一段时间,但我想到的每个解决方案最终都具有相同的长度和复杂性。也许其他人会有更多的运气。。。
xrange <- range(lapply(listOfDataFrames,function(x) range(x$Time)))
mypoints <- 60*do.call(seq,as.list(xrange%/%60))
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