简体   繁体   中英

How to solve: “Error in as.POSIXct.numeric(X[[2L]], …) : 'origin' must be supplied”

we have created the following loop to plot the different parts of the data (with the package "latticeExtra").

library("latticeExtra")
All_dates<-unique(fad$Meet_date)
for (i in 1:11) { Date.i <- All_dates[i]
  layoutno <- unique(fad[fad$Meet_date == Date.i,28] )
  Data.i <- fad[fad$Meet_date == Date.i, ]
  Data.i$newtime <- as.POSIXct(Data.i$DateTime)
  prop <- with(Data.i,tapply(newtime,klasse, max))-with(Data.i,tapply(newtime,klasse, min))
png(paste(Date.i, ".png", sep = ""), height = 20, width = 30, units = "cm", res = 400)
dog <- xyplot(Ratio ~ Meet_times |as.factor(klasse), data = Data.i, main = paste(Date.i), 
layout = c(layoutno,1), scales = list(x = list(relation = "free",format  = "%H:%M")), 
xlab = "Time of measurement", ylab = "Ratio CH4/CO2")
print(resizePanels(dog,w = prop))
dev.off() }

However, we get this: Error in as.POSIXct.numeric(X[[2L]], ...) : 'origin' must be supplied..
I am a big noob with R, so I have no idea where this problem could lie.. My collegue also seems to be lost.. I know it is a big code, but I kinda hoped someone sees the problem rigthaway.. I tried several searches on this website and on Google, but this did not get me anything useful.

Please, help? Thanks a lot in advance!

Well, it's easy to get this problem to go away, but you need to be careful to get the date conversions right. The problem is that you are passing a number to as.POSIXct and not a Date object, and when you pass an integer, you need to give an origin date, so that it can compute the offset. For example:

> date<-as.Date("2013-10-18")
> as.POSIXct(date)
[1] "2013-10-17 20:00:00 EDT"
> date0<-unclass(date)
> date0
[1] 15996
> as.POSIXct(date0)
Error in as.POSIXct.numeric(date0) : 'origin' must be supplied

The real question is, how are you representing your dates in fad$Meet_date (and also why do you want to convert them to POSIXct). The correct origin and conversion will depend on what fad$Meet_date represents.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM