【问题标题】:Setting time intervals for a plot [R]设置绘图的时间间隔 [R]
【发布时间】:2015-07-27 11:32:51
【问题描述】:

致我的previous question,来自数据集,我如何才能在 20 分钟的时间间隔内使用它。

我尝试了两种解决方案,但它们都显示相同的结果。当我尝试将数据集转换为不同的时间间隔(比如 20 分钟)时,我的数据集并未获取所有值。

是否可以将其转换为 data.frame() 而不是 data.table()。这是akrun给出的答案之一:

x    y   date    time
1    2    1-1-01  15:00
2    5    1-1-01  17:00
3    1    1-1-01  18:00
5    7    1-1-01  21:00
2    6    1-1-01  22:00
6    3    1-1-01  23:00
9    2    2-1-01  01:00
6    1    2-1-01  04:00
.....

library(data.table)
DT <- setDT(df1)[, {tmp <- as.numeric(substr(time,1,2))
list(time=sprintf('%02d:00', min(tmp):max(tmp)))}, date]
df1[DT, on=c('date', 'time')]
DT <- setDT(df1)[, list(time=sprintf('%02d:00', 0:23)) , date]
res <- df1[DT, on=c('date', 'time')
         ][,{tmp <- which(!(is.na(x) & is.na(y)))
        .SD[tmp[1L]:tmp[length(tmp)]]}]
res 

library(zoo)
res[, c('x', 'y') :=lapply(.SD, na.approx), .SDcols= x:y]

【问题讨论】:

  • 能把整个代码封装在这里吗?
  • 您的原始数据是一小时间隔。告诉我们 x 和 y 将如何以 20 分钟的间隔按比例划分?或者你有高频数据?
  • @Robert 是的,按比例......我确实有高频数据,但它不是连续的......有时我有分钟数据,但有时我有时间间隔在小时......所以我试图有一个中等的数据集..
  • @RomanLuštrik 我已经封装了代码..你能再看看这个问题吗?谢谢

标签: r


【解决方案1】:

请求运行以下代码...

df1 <- structure(list(x = c(1L, 2L, 3L, 5L, 2L, 6L, 9L, 6L), y = c(2L, 
5L, 1L, 7L, 6L, 3L, 2L, 1L), date = c("1-1-01", "1-1-01", "1-1-01", 
"1-1-01", "1-1-01", "1-1-01", "2-1-01", "2-1-01"), time = c("15:00", 
"17:00", "18:00", "21:00", "22:00", "23:00", "01:00", "04:00"
)), .Names = c("x", "y", "date", "time"), class = "data.frame",
row.names = c(NA, -8L))


library(chron)
library(data.table)


time<-as.character(substr(times(00:71/72),1,5))
dates <- paste0(1:2,'-1-01')

all.dt <- expand.grid(date=dates,time=time)
big.data <- merge(all.dt, df1, all.x=TRUE)

现在对于最后一部分,您可以通过运行以下代码来填充 NA

library(zoo)
big.data <- within(big.data,{
         x <- na.approx(x,na.rm=FALSE)
         y <- na.approx(y,na.rm=FALSE)
})

【讨论】:

  • @Fairy 你可以删除library(data.table)。在整个代码中data.table 没有在任何地方使用。此外,输出big.data 是一个数据框(检查is.data.frame(big.data))。由于您的日期不同,您可以删除dates &lt;- paste0(1:2,'-1-01') 行并使用seq(as.Date(date,format),as.Date(date,format),by = "day") 生成日期变量序列。
  • Error in as.Date.default(date, format) : do not know how to convert 'date' to class “Date” 创建序列时出现错误。
【解决方案2】:

使用 xts 试试这个。我使用了某种不同的数据来“查看”结果:

indata <- read.table(text='x    y    date    time
1    2    1-1-01  15:00
2    2    1-1-01  15:19
                      2    5    1-1-01  17:00
                      3    1    1-1-01  17:05
                      3    1    1-1-01  18:00
                      3    1    1-1-01  18:20
                      5    7    1-1-01  21:05
                      6    6    1-1-01  21:08
                      2    6    1-1-01  22:00
                      6    3    1-1-01  23:11
                      9    2    2-1-01  1:00
                      9    2    2-1-01  1:21
                      6    1    2-1-01  4:29
                      ', header=TRUE,stringsAsFactors = F)


library(xts)
xt <- strptime(paste(indata$date,indata$time),
               "%d-%m-%y %H:%M")
its=xts(x = indata[,1:2],
    order.by = xt,
    frequency = NULL)

period.apply(its, INDEX=endpoints(xt, on="minutes", k=20), FUN=mean) 

                      x   y
2001-01-01 15:19:00 1.5 2.0
2001-01-01 17:05:00 2.5 3.0
2001-01-01 18:00:00 3.0 1.0
2001-01-01 18:20:00 3.0 1.0
2001-01-01 21:08:00 5.5 6.5
2001-01-01 22:00:00 2.0 6.0
2001-01-01 23:11:00 6.0 3.0
2001-01-02 01:00:00 9.0 2.0
2001-01-02 01:21:00 9.0 2.0
2001-01-02 04:29:00 6.0 1.0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    • 2020-06-21
    • 1970-01-01
    相关资源
    最近更新 更多