0

我目前有一个矩阵如下:

mat
   00:00   1
   00:05   2
   00:07   4
   etc...

包含一天中某些感兴趣的事件发生的时间。但是,我希望在该矩阵的每一分钟都填充该表,从而生成如下矩阵:

mat
   00:00   1
   00:01   0
   00:02   0
   00:03   0
   00:04   0
   00:05   2
   00:06   0
   00:07   4
   etc...

有没有一种简单的方法可以在 R 中做到这一点?

供您参考,`dput(head(mat))' 返回:

structure(list(Var1 = structure(1:6, .Label = c("00:00", "00:05", "00:07", "05:52", "16:28", "19:34", "19:55", "20:01", "20:40", "23:29", "23:56"), class = "factor"), Freq = c(1L, 2L, 4L, 1L, 1L, 1L)), .Names = c("Var1", "Freq"), row.names = c(NA, 6L), class = "data.frame")

4

3 回答 3

2

毫无疑问,有更好的方法可以做到这一点,但我想到了一个简单的工具:merge.

在这里,我正在合并字符向量,但如果您真的在处理时间,我建议您使用实际时间格式:

# Something that resembles your input data.frame
df1 <- data.frame(V1 = c("00:00", "00:05", "00:07"), 
                  V2 = c(1, 2, 4), stringsAsFactors = FALSE)
# Another data.frame of just a single column of the complete time range
df2 <- data.frame(V1 = sprintf("00:%02d", 0:10), 
                  stringsAsFactors = FALSE)


# Now, merge them
out <- merge(df1, df2, all.y = TRUE, sort = TRUE)
# Replace NA with 0
out[is.na(out)] <- 0

# View your result
out
#       V1 V2
# 1  00:00  1
# 2  00:01  0
# 3  00:02  0
# 4  00:03  0
# 5  00:04  0
# 6  00:05  2
# 7  00:06  0
# 8  00:07  4
# 9  00:08  0
# 10 00:09  0
# 11 00:10  0
于 2013-07-24T16:54:35.997 回答
2

这个想法是将 amerge与一系列日期一起使用。但我认为这里的困难在于创建一个时间序列。

这里有一个方法:

seq <- seq.POSIXt(as.POSIXct("2000/1/1"), length.out=10,
          by = as.difftime(1,unit='mins'))
dt2 <- data.frame(V1=format(seq,'%H:%M'))
   V1
1  00:00
2  00:01
3  00:02
4  00:03
5  00:04
6  00:05
7  00:06
8  00:07
9  00:08
10 00:09

然后使用merge

dt1<- read.table(text='00:00   1
00:05   2
00:07   4')
res <- merge(dt1,dt2,all.y=TRUE)
          V1 V2
1  00:00  1
2  00:05  2
3  00:07  4
4  00:01 NA
5  00:02 NA
6  00:03 NA
7  00:04 NA
8  00:06 NA
9  00:08 NA
10 00:09 NA

OP数据澄清后编辑:

# dat
# Var1 Freq
# 1 00:00    1
# 2 00:05    2
# 3 00:07    4
# 4 05:52    1
# 5 16:28    1
# 6 19:34    1
##  convert factor to string , better to get ordered merge
dat$Var1 <- as.character(dat$Var1)
## compute number of minutes 
Nmins <- difftime(as.POSIXct(tail(dat$Var1,1),format='%H:%M'),
                  as.POSIXct(head(dat$Var1,1),format='%H:%M'),units='mins')
## create sequence
seq <- seq.POSIXt(as.POSIXct("2000/1/1"), length.out=as.numeric(Nmins),
                  by = as.difftime(1,unit='mins'))
dt2 <- data.frame(V1=format(seq,'%H:%M'))

merge(dat,dt2,all.y=TRUE,by.x='Var1',by.y='V1')
于 2013-07-24T16:54:56.560 回答
0

我的 2 美分值得使用动物园:

aaa<-zoo(c(1,2,4), times(c("0:00:00", "0:05:00", "0:07:00")))
aa1<-seq(min(time(aaa)), max(time(aaa)), by=times("00:01:00"))
aa2<-zoo(rep(0, length(aa1)), aa1)
aa3<-merge(aaa,aa2)
aa4<-aa3$aaa+aa3$aa2
aa4[is.na(aa4)] <- 0

不是太漂亮,但似乎工作......

于 2013-07-24T17:05:48.893 回答