2015-02-07 68 views
0

我正試圖生成一個新的數據框,其中包含基於數據框中的一行的每週遇到動物的歷史記錄,當我們停止跟蹤它(命運)以及其他協變量時,動物ID(BandNo)首先和最後一天跟蹤它(FDay,Lday),以及動物的命運。在lapply中生成並填充新的數據幀 - 「不知道如何將x轉換爲POSIXlt類」

這裏是一個單獨的,對象的示例性數據幀的「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)) 

但當我嘗試使用以下代碼將其應用於單行數據幀列表時,出現錯誤消息「不知道如何將x轉換爲POSIXlt類」

上一步,在lap現在,給錯誤

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)}) 

感謝您的任何幫助!

+0

這裏列出的代碼有幾個問題。首先,你需要一個打開和關閉的支架。如果你現在執行代碼,它將是一個語法錯誤,然後你很可能會看到一個「Error:object'x'not found」,因爲在解釋器嘗試評估下一行語法錯誤之後,在這一點上沒有找到x。你可以修復代碼,以便評估它。 – jimmyb 2015-02-07 20:07:46

+0

我做了這些改變,對不起。現在有一個不同的錯誤 「as.POSIXlt.default(x,tz = tz(x))中的錯誤: 不知道如何將'x'轉換爲類'POSIXlt' 是否像定義一個as.POSIXct在某處? – tlyons253 2015-02-07 20:33:44

+0

'x $ mydate'是'NULL',因爲'x'中沒有列'mydate','NULL'不能轉換爲'POSIXct'。也許它應該是'mydate'而不是'x $ mydate' – NicE 2015-02-07 21:31:03

回答

0

我犯了一個真正的骨頭錯誤......我應該在最後引用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)} 
) 
相關問題