我正在尝试切割两个数据帧,一个表示日期(Date),另一个表示连续值,并使用xts线图对它们进行绘图。当我使用完整的日期帧时它可以工作,但是当我切换到最后7个值时,它会抛出以下错误。有什么想法吗?
段中的错误(xlim [1],y_grid_lines(get_ylim()[[2]]),xlim [2],y_grid_lines(get_ylim()[[2]]),: 不能混合零长度和非零长度坐标
library(xts)
Date <- seq(as.Date("2018-01-01"), as.Date("2018-02-25"), by="days")
Date <- as.POSIXct(Date, format = "%Y-%m-%d %H:%M")
#Date <-tail #Works
Date <-tail(Date,7) #Doesn't Work
T1_EF <- matrix( rnorm(N*M,mean=23,sd=3), N, M)
Trial1_EF<- as.matrix(round(Trial1_EF, digits = 6))
T1_EF_counts <- apply(Trial1_EF, 2, function(x) length(na.omit(x)))
#c <-T1_EF_counts #Works
c <-tail(T1_EF_counts,7)#doesn't work
datN <- data.frame(Date = Date, y = c)
datN.ts <- xts(datN$y, order.by = datN$Date)
plot(datN.ts)
lines(datN.ts, col = 'Green')
答案 0 :(得分:1)
我在这里找到了一个解决方案: https://github.com/joshuaulrich/xts/issues/156
当y值是常数时,xls不喜欢。
这是编辑过的代码:
library(xts)
Date <- seq(as.Date("2018-01-01"), as.Date("2018-02-25"), by="days")
Date <- as.POSIXct(Date, format = "%Y-%m-%d %H:%M")
#Date <-tail #Works
Date <-tail(Date,7) #Doesn't Work
T1_EF <- matrix( rnorm(N*M,mean=23,sd=3), N, M)
Trial1_EF<- as.matrix(round(Trial1_EF, digits = 6))
T1_EF_counts <- apply(Trial1_EF, 2, function(x) length(na.omit(x)))
#c <-T1_EF_counts #Works
c <-tail(T1_EF_counts,7)#doesn't work
datN <- data.frame(Date = Date, y = c)
datN.ts <- xts(datN$y, order.by = datN$Date)
plot(datN.ts, , ylim=c(300, 600))
lines(datN.ts, col = 'Green')