设置绘图的时间间隔[R]

设置绘图的时间间隔[R],r,R,对于我的,从数据集,我如何在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

对于我的,从数据集,我如何在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]
xy日期时间
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
.....
库(数据表)

DT请求运行以下代码

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)

df1使用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

indata您能把整个代码封装在这里吗?您的原始数据是每隔一小时一次。告诉我们,在20分钟的时间间隔内,x和y是如何按比例划分的?还是你有高频数据?@Robert是的,按比例。。我有高频数据,但它不是连续的。。。有时我有分钟数据,但有时我有以小时为单位的时间间隔。。因此,我试图获得一个适度的数据集。@RomanLuštrik我已经封装了代码。。请你再看一遍这个问题好吗。感谢you@Fairy您可以删除
库(data.table)
。在整个代码
数据中。表
未在任何地方使用。另外,输出的
big.data
是一个数据帧(选中
is.data.frame(big.data))
。由于您的日期是可变的,您可以删除as.date.default(日期,格式)中的行
dates
错误:不知道如何将“date”转换为类“date”
在创建序列时给我错误。
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