English 中文(简体)
我如何按时间间隔对数据框架进行对比?
原标题:How do I match data frames by time intervals?
  • 时间:2010-11-09 21:50:45
  •  标签:
  • r
  • zoo
  • xts

在我从数据记录中进口原始数据时,这个问题经常出现。 温度记录将每10分钟记录温度,并且将另立气记录,记录过去10分钟间使用的气体。 我想将这两个记录仪的数据合并为单一数据框架,用于进行绘图和分析,但时间并没有完全一致。 我想在每10分钟的数据框架内各有一行,时间显示时间段的开始。

温度记录数据如下:

           datetime temperature
2010-09-30 06:58:53 78.996
2010-09-30 07:08:53 78.645
2010-09-30 07:18:53 78.514
2010-09-30 07:28:53 79.173
2010-09-30 07:38:53 78.602

气态数据如下:

           datetime gas
2010-09-30 13:45:00  0
2010-09-30 13:55:00  1
2010-09-30 14:05:00  0
2010-09-30 14:15:00  4
2010-09-30 14:25:00  2

我想将两个数据框架每10分钟合并,以便综合数据看上去:

           datetime temperature gas  
2010-09-30 13:40:00 NA          0
2010-09-30 13:50:00 78.996      1
2010-09-30 14:00:00 78.645      0
2010-09-30 14:10:00 78.514      4
2010-09-30 14:20:00 79.173      2
2010-09-30 07:38:53 78.602      NA

这里有一些密码来获取这两个数据框架:

temps <- data.frame(datetime=c("2010-09-30 06:58:53",
"2010-09-30 07:08:53","2010-09-30 07:18:53",
"2010-09-30 07:28:53","2010-09-30 07:38:53"),
 temperature=c(78.996,78.645,78.514,79.173,78.602),
stringsAsFactors=FALSE)
temps$datetime <- strptime(temps$datetime, format="%Y-%m-%d %H:%M:%S")
gas <- data.frame(datetime=c("2010-09-30 13:45:00",
"2010-09-30 13:55:00","2010-09-30 14:05:00",
"2010-09-30 14:15:00","2010-09-30 14:25:00"),
gas=c(0,1,0,4,2),stringsAsFactors=FALSE)
gas$datetime <- strptime(temps$datetime, format="%Y-%m-%d %H:%M:%S")
最佳回答

http://cran.r-project.org/ Package=xts>。

library(xts)
xTemps <- align.time(xts(temps[,2],as.POSIXct(temps[,1])), n=600)
xGas <- align.time(xts(gas[,2],as.POSIXct(gas[,1])), n=600)
merge(xTemps,xGas)
问题回答

使用zooxts 持有您的数据——其<代码>合并(>)功能将作您。 你可以在此寻求以前的答复。 zoo文件也有许多相关实例。

这里是一个模拟例子:

> set.seed(42)
> temps <- zoo(78+rnorm(5), 
+              order.by=ISOdatetime(2010,9,30,6,58,53)+cumsum(60*runif(5)))
> gas <- zoo(sample(1:5,5), 
+            order.by=ISOdatetime(2010,9,30,6,58,53)+cumsum(60*runif(5)))
> merge(temps,gas)
                     temps gas
2010-09-30 06:59:47 78.048  NA
2010-09-30 06:59:49     NA   5
2010-09-30 07:00:44 76.895  NA
2010-09-30 07:00:48     NA   1
2010-09-30 07:00:55     NA   3
2010-09-30 07:01:01 78.539  NA
2010-09-30 07:01:23     NA   2
2010-09-30 07:01:51 78.580  NA
2010-09-30 07:01:57     NA   4
2010-09-30 07:02:29 77.342  NA
> na.locf(merge(temps,gas))
                     temps gas
2010-09-30 06:59:49 78.048   5
2010-09-30 07:00:44 76.895   5
2010-09-30 07:00:48 76.895   1
2010-09-30 07:00:55 76.895   3
2010-09-30 07:01:01 78.539   3
2010-09-30 07:01:23 78.539   2
2010-09-30 07:01:51 78.580   2
2010-09-30 07:01:57 78.580   4
2010-09-30 07:02:29 77.342   4
> 




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签