1
似乎lapply
軋液機POSIXlt
記錄:爲什麼lapply mangle POSIXlt?
> hours
[1] "2016-01-01 00:00:00 GMT" "2016-01-01 01:00:00 GMT" "2016-01-01 02:00:00 GMT"
> str(hours)
POSIXlt[1:3], format: "2016-01-01 00:00:00" "2016-01-01 01:00:00" "2016-01-01 02:00:00"
> dput(hours)
structure(list(sec = c(0, 0, 0), min = c(0L, 0L, 0L), hour = 0:2,
mday = c(1L, 1L, 1L), mon = c(0L, 0L, 0L), year = c(116L,
116L, 116L), wday = c(5L, 5L, 5L), yday = c(0L, 0L, 0L),
isdst = c(0L, 0L, 0L)), .Names = c("sec", "min", "hour",
"mday", "mon", "year", "wday", "yday", "isdst"), class = c("POSIXlt",
"POSIXt"), tzone = "GMT")
> read.csv(file.path(data.dir,strftime(hours[1],"%Y%d%m%H.csv")))
.... success ....
> lapply(hours, function (h) read.csv(file.path(data.dir,strftime(h,"%Y%d%m%H.csv"))))
Error in as.POSIXlt.numeric(x, tz = tz) (from #1) : 'origin' must be supplied
Calls: lapply -> FUN -> read.csv -> read.table -> file.path -> strftime -> format -> as.POSIXlt -> as.POSIXlt.numeric
> lapply(hours,function(h) {
+ print(h)
+ str(h)
+ read.csv(file.path(data.dir,strftime(h,"%Y%m%d%H.csv")))
+ })
+ . + [1] 0 0 0
num [1:3] 0 0 0
Error in as.POSIXlt.numeric(x, tz = tz) (from #1) : 'origin' must be supplied
IOW,在函數的hours
元件內部lapply
是數值向量,不POSIXlt
。
它看起來像hours
是個人POSIXlt
字段的列表,而不是一個向量或POSIXlt
對象。
我做錯了什麼?
PS。下面似乎是一個解決方法:
lapply(1:length(hours),function(i) ....hours[i]...)