在 lapply-"do not know how to convert x to class POSIXlt" 中生成并填充新的数据帧

generating and filling new data frames in lapply-"do not know how to convert x to class POSIXlt"

我正在尝试根据数据框中的一行生成一个包含动物每周遭遇历史的新数据框,该数据框包含我们跟踪它的第一天和最后一天(FDay、Lday)的动物 ID (BandNo),以及当我们停止追踪动物时动物的命运(命运)以及其他协变量。

这里是一个个体的示例数据框,对象 "a"

structure(list(BandNo = structure(1L, .Label = c("1234", "4201", 
"4203", "4205", "4207", "4208", "4209", "4213", "4214", "4215", 
"4216", "4217", "4219", "4221", "4223", "4224", "4226", "4227", 
"4228", "4229", "4230", "4231", "4232", "4233", "4234", "4236", 
"4237", "4238", "4239", "4241", "4242", "4245", "4247", "4248", 
"4249", "4253", "4254", "4256", "4257", "4258", "4259", "4261", 
"4262", "4263", "4264", "4271", "4272", "4273", "4276", "4277", 
"4280", "4282", "4284", "4288", "4289", "4292", "4293", "4296", 
"4298", "4299", "4501", "4502", "4503", "4504", "4505", "4507", 
"4508", "4509", "4510", "4511", "4512", "4513", "4514", "4515", 
"4516", "4517", "4518", "4519", "4520", "4521", "4525", "4526", 
"4527", "4529", "4530", "4532", "4535", "4539", "4596", "4598", 
"4599", "6101", "6102", "6104", "6105", "6106", "6107", "6108", 
"6109", "6111", "6112", "6113", "6114", "6115", "6116", "6118", 
"6119", "8002", "8003", "8004", "8005", "8006", "8007", "8008", 
"8009", "8010", "8011", "8012", "8013", "8014", "8015", "8017", 
"8018", "8019", "8020", "8021", "8097", "8098", "8099", "8402", 
"8403", "8404", "8405", "8406", "8408", "8409", "8410", "8411", 
"8412", "8413", "8414", "8416", "8417", "8418", "8419", "8422", 
"8423", "8426", "8427", "8429", "8430", "8431", "8432", "8433", 
"8458", "8497", "8498"), class = "factor"), FDay = structure(1380171600, class = c("POSIXct", 
"POSIXt"), tzone = "America/Bogota"), Lday = structure(1392094800, class = c("POSIXct", 
"POSIXt"), tzone = "America/Bogota"), ObsLength = 138, Fate = "Predation", 
    FieldName = structure(7L, .Label = c("Bryan", "Dassow", "H1", 
    "H2", "NARD", "SAY160", "SAY320", "SAY40A", "Schaeffer", 
    "SIB", "Wessels"), class = "factor"), Landscape = structure(2L, .Label = c("CHW", 
    "SAY", "SIB"), class = "factor"), Sex = structure(1L, .Label = c("F", 
    "M"), class = "factor")), .Names = c("BandNo", "FDay", "Lday", 
"ObsLength", "Fate", "FieldName", "Landscape", "Sex"), row.names = 1L, class = "data.frame")

我可以使用此代码成功创建我想要的新数据框 (y) :

library(lubridate)
mydate<-seq(from=a$FDay,to=a$Lday,by='week')
newband<-rep(a$BandNo,length(mydate))
newfate<-rep("Survive",length(mydate))
newfate[length(mydate)]<-a$Fate
y<-data.frame(newband,mydate,newfate)
y$FieldName<-a$FieldName
y$Sex<-a$Sex
y$Landscape<-a$Landscape
y$WeekID<-week(a$mydate)
y$Year<-year(a$mydate))

但是当我尝试使用以下代码将它应用于单行数据框列表时,我收到错误消息 "do not know how to convert x to class POSIXlt"

之前的步骤,现在用在lapply,报错

b<-list(a)
d<-lapply(b,function(x){
mydate<-seq(from=x$FDay,to=x$Lday,by='week')
newband<-rep(x$BandNo,length(mydate))
newfate<-rep("Survive",length(mydate))
newfate[length(mydate)]<-x$Fate
y<-data.frame(newband,mydate,newfate)
y$FieldName<-x$FieldName
y$Sex<-x$Sex
y$Landscape<-x$Landscape
y$WeekID<-week(x$mydate)
y$Year<-year(x$mydate)})

感谢您的帮助!

我犯了一个非常愚蠢的错误...我应该在最后引用 y$ mydate。 lapply 函数应该是这样的:

d<-lapply(adult2,function(x){
mydate<-seq(from=x$FDay,to=x$Lday,by='week')
newband<-rep(x$BandNo,length(mydate))
newfate<-rep("Survive",length(mydate))
newfate[length(mydate)]<-x$Fate
y<-data.frame(newband,mydate,newfate)
y$FieldName<-x$FieldName
y$Sex<-x$Sex
y$Landscape<-x$Landscape
y$WeekID<-week(y$mydate)
y$Year<-year(y$mydate)
return(y)}
)