本文介绍了如何填写(date-)data.frame中的空白?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有日期列的data.frame。这些日期可能会发生多次,但也可以是零时间:

 日期值
1 2013-01-01 5
2 2013-01-01 3
3 2013-01-03 3
4 2013-01-04 3
5 2013-01-04 1
6 2013 -01-06 1

如何填写这个数据框架中的日期间隔,所以我得到以下?

 日期值
1 2013-01-01 5
2 2013-01-01 3
3 2013-01-02 0
4 2013-01-03 3
5 2013-01-04 3
6 2013-01-04 1
7 2013-01-05 0
8 2013-01-06 1

欢迎任何帮助。



TIA,
Jerry

解决方案

合并您的数据框架与另一个数据框架包含所有日期的顺序。这里我假设dat是你原始的数据框架。

  hh<  -  data.frame(date = seq(as.Date (2013-01-01),as.Date(2013-01-6),by =days))
> res< - merge(dat,hh,by.x = 'date',by.y ='date',all.x = T,all.y = T)
日期值
1 2013-01-01 5
2 2013-01- 01 3
3 2013-01-02 NA
4 2013-01-03 3
5 2013-01-04 3
6 2013-01-04 1
7 2013-01-05 NA
8 2013-01-06 1

现在我们有在dat中没有匹配行的每个行的NA为h。 Personaly,我认为最好不要说这些缺少值但是你可以将它们设置为0:

  res $ value [is.na(res $ value)]<  -  0 

/ strong>



通用性可以生成hh,如@Arun解决方案所示:

 code> hh<  -  seq(min(dat $ date),max(dat $ date),by =days)


I have a data.frame with a date-column. These dates can occur many times, but also zero-time:

        date value
1 2013-01-01     5
2 2013-01-01     3
3 2013-01-03     3
4 2013-01-04     3
5 2013-01-04     1
6 2013-01-06     1

How do I fill the date-gaps in this data.frame so I get the following?

        date value
1 2013-01-01     5
2 2013-01-01     3
3 2013-01-02     0
4 2013-01-03     3
5 2013-01-04     3 
6 2013-01-04     1
7 2013-01-05     0
8 2013-01-06     1

Any help is welcome.

TIA,Jerry

解决方案

You can merge your data.frame with another data.frame containg all the dates in sequence. here I assume that dat is your original data.frame.

hh<- data.frame(date=seq(as.Date("2013-01-01"), as.Date("2013-01-6"), by="days"))
>res <- merge(dat,hh,by.x='date',by.y='date',all.x=T,all.y=T)
        date value
1 2013-01-01     5
2 2013-01-01     3
3 2013-01-02    NA
4 2013-01-03     3
5 2013-01-04     3
6 2013-01-04     1
7 2013-01-05    NA
8 2013-01-06     1

Now we have NA for each row in dat that has no matching row in hh. Personaly, I think it is better to have NA to say that theses are missing values But you can set them to 0:

res$value[is.na(res$value)] <- 0

Edit

for generality you can generate hh as shown in @Arun solution:

      hh <- seq(min(dat$date), max(dat$date), by="days")

这篇关于如何填写(date-)data.frame中的空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 14:47