根据开始date.time和结束date.time创建一个新的date.time“ interval”列

时间:2018-11-16 03:45:49

标签: r datetime intervals lubridate

我有一个数据集的开始和结束时间,这些数据是礁石在退潮时露出的时间。我正在尝试使用lubridate :: interval函数创建一个名为“ interval”的新列。我有大约400行数据。

https://github.com/LCRoysterproject/bird/blob/STEVEN-L--DEVELOPMENT/data/birdcamtides.csv

  Site     Start.Date.Time       End.Date.Time

1个LCOR_1 2017-11-08 13:20:00 2017-11-08 15:00:00 2 LCOR_1 2017-11-09 08:40:00 2017-11-09 15:45:00 3 LCOR_1 2017-11-10 09:20:00 2017-11-10 18:30:00 4 LCOR_1 2017-11-11 10:00:00 2017-11-11 18:55:00 5 LCOR_1 2017-11-12 12:05:00 2017-11-12 18:30:00 6 LCOR_1 2017-11-13 13:40:00 2017-11-13 18:55:00 7 LCOR_1 2017-11-14 07:35:00 2017-11-14 09:10:00 8 LCOR_1 2017-11-14 14:45:00 2017-11-14 18:55:00 9 LCOR_1 2017-11-15 07:30:00 2017-11-15 10:30:00 10 LCOR_1 2017-11-15 15:50:00 2017-11-15 18:55:00

library(lubridate)
tides <- read.csv("data/birdcamtides.csv", header = T)

tides$Start.Date.Time <- paste(tides$Date, tides$Start.Low.Tide)
tides$End.Date.Time <- paste(tides$Date, tides$End.Low.Tide)

tides$Start.Date.Time <- mdy_hm(tides$Start.Date.Time, tz = 'EST')
tides$End.Date.Time <- mdy_hm(tides$End.Date.Time, tz = 'EST')

tides$interval <- interval(tides$Start.Low.Tides,
tides$End.Low.Tide)

我不断收到错误消息 “ as.POSIXlt.character(as.character(x),...)中的错误:   字符串不是标准的明确格式”

我不知道如何收到此错误,因为我所有的日期都是POSIX.ct格式。我的问题有所不同,因为我试图编写一个函数或类似的东西来创建一个新列,并为每个对应的开始和结束时间设置一个间隔。

1 个答案:

答案 0 :(得分:0)

dput:

tides <- structure(list(Date = structure(c(15L, 16L, 1L, 2L, 3L, 4L, 5L, 
5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 10L, 11L, 12L, 13L, 14L), .Label = c("11/10/2017", 
"11/11/2017", "11/12/2017", "11/13/2017", "11/14/2017", "11/15/2017", 
"11/16/2017", "11/17/2017", "11/18/2017", "11/19/2017", "11/20/2017", 
"11/21/2017", "11/22/2017", "11/23/2017", "11/8/2017", "11/9/2017"
), class = "factor"), Site = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "LCOR_1", class = "factor"), 
    Camera = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "HC500", class = "factor"), 
    Start.Low.Tide = structure(c(3L, 13L, 14L, 1L, 2L, 4L, 10L, 
    5L, 9L, 6L, 9L, 7L, 9L, 8L, 9L, 10L, 9L, 11L, 11L, 12L), .Label = c("10:00", 
    "12:05", "13:20", "13:40", "14:45", "15:50", "16:40", "17:55", 
    "7:30", "7:35", "7:40", "8:00", "8:40", "9:20"), class = "factor"), 
    End.Low.Tide = structure(c(10L, 11L, 12L, 14L, 12L, 14L, 
    15L, 14L, 2L, 13L, 1L, 13L, 4L, 13L, 3L, 5L, 8L, 6L, 7L, 
    9L), .Label = c("10:25", "10:30:00", "10:45", "10:55", "12:20", 
    "12:50", "13:25", "13:40", "14:35", "15:00:00", "15:45:00", 
    "18:30:00", "18:55", "18:55:00", "9:10:00"), class = "factor"), 
    Notes = structure(c(1L, 1L, 1L, 3L, 1L, 3L, 2L, 3L, 2L, 3L, 
    2L, 3L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("", "reef showing on the first frame of the day", 
    "reef showing on the last frame of the day"), class = "factor"), 
    Start.Date.Time = structure(c(1510165200, 1510234800, 1510323600, 
    1510412400, 1510506300, 1510598400, 1510662900, 1510688700, 
    1510749000, 1510779000, 1510835400, 1510868400, 1510921800, 
    1510959300, 1511008200, 1511094900, 1511181000, 1511268000, 
    1511354400, 1511442000), class = c("POSIXct", "POSIXt"), tzone = "EST"), 
    End.Date.Time = structure(c(NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, 1510790100, 1510845900, 1510876500, 1510934100, 1510962900, 
    1511019900, 1511112000, 1511203200, 1511286600, 1511375100, 
    1511465700), class = c("POSIXct", "POSIXt"), tzone = "EST")), .Names = c("Date", 
"Site", "Camera", "Start.Low.Tide", "End.Low.Tide", "Notes", 
"Start.Date.Time", "End.Date.Time"), row.names = c(NA, 20L), class = "data.frame")

间隔的变量是错误的。应该是:tides$interval <- interval(tides$Start.Date.Time, tides$End.Date.Time),因为这是您计算出的润滑的版本。