Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.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的数据帧中插入零_R_Datetime_Dataframe - Fatal编程技术网

如何在R的数据帧中插入零

如何在R的数据帧中插入零,r,datetime,dataframe,R,Datetime,Dataframe,我有下面的data.frame,DF DF已经在R中。我们不需要使用read.csv或其他方式将其加载到R中 时间戳计数 1 2014-01-15 14:30:00 2 2 2014-01-15 16:30:00 3 3 2014-01-15 17:00:00 2 4 2014-01-15 17:15:00 1 我有一个“独立的时间戳序列”,比如从2014-01-15 14:00:00到2014-01-22 13:00:00。我想从data.frame中获取计数列

我有下面的data.frame,DF DF已经在R中。我们不需要使用read.csv或其他方式将其加载到R中

时间戳计数 1 2014-01-15 14:30:00 2 2 2014-01-15 16:30:00 3 3 2014-01-15 17:00:00 2 4 2014-01-15 17:15:00 1
我有一个“独立的时间戳序列”,比如从
2014-01-15 14:00:00
2014-01-22 13:00:00
。我想从data.frame中获取计数列表,并为data.frame中不存在但在
tmpSeq中存在的
时间戳插入零,假设您的序列以15分钟为增量:

DF <- data.frame(timeStamp=as.POSIXct(c("2014-01-15 14:30:00","2014-01-15 16:30:00",
                                        "2014-01-15 17:00:00","2014-01-15 17:15:00")),
                 count=c(2,3,2,1))


tmpSeq <- seq(as.POSIXct("2014-01-15 14:00:00"),
              as.POSIXct("2014-01-22 13:00:00"), by="15 mins")

DF <- merge(DF, data.frame(timeStamp=tmpSeq, count=0), all=TRUE)

DF看来你要找的是“合并”。请看这篇文章:

您需要一个正确的外部联接(如果您将tmpSeq作为正确的数据帧)

编辑: 在答案中添加merge语句以使答案更清晰:

Right outer: merge(x = DF, y = data.frame(timeStamp=tmpSeq, count=0), all.y=TRUE)

通常,在处理时间序列对象时,最好使用一些ts包。使用
xts
包,您可以使用
rbind
合并2个时间序列

  • 首先,我创建短时间序列
  • 我生成长ts,假设它是间隔15分钟的规则ts
  • 我使用
    rbind
这是我的代码:

library(xts)
dat = as.xts(read.zoo(text='
time Stamp count        ## a small hack here to read your data
1 2014-01-15 14:30:00     2
2 2014-01-15 16:30:00     3
3 2014-01-15 17:00:00     2
4 2014-01-15 17:15:00     1',
         header=TRUE,
         index=1:2,
         format='%Y-%m-%d %H:%M:%S',tz=''))

## generate the long ts
tmpSeq <-
seq.POSIXt(as.POSIXct('2014-01-15 14:00:00'),
    as.POSIXct('2014-01-22 13:00:00'),by = '15 mins')
tmpSeq <- 
xts(x=rep(0,length(tmpSeq)),tmpSeq)

## insert dat values in tmpSeq
rbind(tmpSeq,dat)
库(xts)
dat=as.xts(read.zoo(text=)
时间戳计数###这里有一个小黑客可以读取您的数据
1 2014-01-15 14:30:00     2
2 2014-01-15 16:30:00     3
3 2014-01-15 17:00:00     2
4 2014-01-15 17:15:00     1',
header=TRUE,
指数=1:2,
格式=“%Y-%m-%d%H:%m:%S”,tz=”)
##生成长ts

tmpSeq您应该提供一个可复制的示例,并展示您迄今为止尝试过的内容。谢谢各位。合并功能实际上起到了帮助作用。
library(xts)
dat = as.xts(read.zoo(text='
time Stamp count        ## a small hack here to read your data
1 2014-01-15 14:30:00     2
2 2014-01-15 16:30:00     3
3 2014-01-15 17:00:00     2
4 2014-01-15 17:15:00     1',
         header=TRUE,
         index=1:2,
         format='%Y-%m-%d %H:%M:%S',tz=''))

## generate the long ts
tmpSeq <-
seq.POSIXt(as.POSIXct('2014-01-15 14:00:00'),
    as.POSIXct('2014-01-22 13:00:00'),by = '15 mins')
tmpSeq <- 
xts(x=rep(0,length(tmpSeq)),tmpSeq)

## insert dat values in tmpSeq
rbind(tmpSeq,dat)