似乎lapply
mangles 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]...)
答案 0 :(得分:4)
operator<
个对象是一个命名的向量列表(参见#include <tuple> // for std::tie
class Time {
...
public:
friend bool operator<(const Time& a, const Time& b) {
return std::tie(a.hour, a.minute) < std::tie(b.hour, b.minute);
}
};
)。 POSIXlt
循环遍历列表,并在每个向量(?POSIXlt
,lapply
,...,FUN
)上调用"sec"
。
这类似于"min"
个对象是向量列表的方式,因此"isdst"
遍历data.frame的列并在每个向量上调用data.frame
。
更好的解决方法是将lapply
对象转换为FUN
,除非您需要POSIXlt
中的特定功能。