as.POSIXct错误找不到bug

时间:2015-02-11 21:04:09

标签: r date time timestamp posixct

我正在尝试将我的日期和时间转换为R recognsied以对数据进行排序的格式。我有一个与以前的数据集一起工作的代码,但在这里我找不到为什么它不起作用。这可能是一件简单的事情,但我感到沮丧 这是我试图在名为DataLHB的数据集上运行的代码:

head(DataLHB)
        date_hour month_num animal_id   name sex Receiver detection_count presence_flag
       1 2009060912         6        42 Ellama   F   101739               1             1
       2 2009060912         6       224  Pokey   F       NA               0             0
       3 2009060913         6        42 Ellama   F   101739               5             1
       4 2009060913         6       224  Pokey   F   101739              11             1     

代码是

sDataLHB <- split(DataLHB, DataLHB$name) ##order the data by individual names
sDataLHB <- lapply(sDataLHB, function(Ind) {
  Ind <- subset(Ind, presence_flag == 1, select = date_hour)
  Ind <- within(Ind, {
   year <- substring(date_hour, 1, 4)
   month <- substring(date_hour, 5, 6)
   day <- substring(date_hour, 7, 8)
   H <- substring(date_hour, 9, 10)
   M <- S <- "00"
   datehour<-paste(paste(year, month, day, sep = "-"),
           paste(H, M, S, sep = ":"))
   RTime <- as.POSIXct(datehour)
   })
  Ind
 })

 **Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard unambiguous format** 

我尝试添加

 as.POSIXct(datehour, format="%Y-%m-%d %H:%M:%S")

但这只会给我警告

    There were 18 warnings (use warnings() to see them)
    Warning messages:
    1: In `[<-.data.frame`(`*tmp*`, nl, value = structure(list( ... : replacement element 1 has 1 row to replace 0 rows
    2: In `[<-.data.frame`(`*tmp*`, nl, value = structure(list( ... : replacement element 2 has 1 row to replace 0 rows
    3: In `[<-.data.frame`(`*tmp*`, nl, value = structure(list( ... : replacement element 3 has 1 row to replace 0 rows

如果有人能够启发我,我将非常感激!

由于

0 个答案:

没有答案