Search code examples
rposixct

Unable to locate a POSIXct object in a column of a data frame in R


I have a data frame df such that

df(head)

gives the following result: first 6 records with dateTime in UTC. The output for

dput(head(df))

is

structure(list(timeGapBurst = c(0L, 0L, 0L, 0L, 0L, 0L), TAG = c(972006000211, 
972006000211, 972006000211, 972006000211, 972006000211, 972006000211
), TIME = c(1593702012718, 1593702020718, 1593702028717, 1593702036717, 
1593702044716, 1593702052716), X = c(242494.137764549, 242495.260779439, 
242491.847125332, 242485.638122567, 242486.991011787, 242491.723274756
), Y = c(716657.708642993, 716658.062463064, 716661.152784716, 
716665.96107613, 716665.02849546, 716662.377185992), Z = c(-64, 
-64, -64, -63, -63, -64), VARX = c(8.82908, 4.03247, 2.99913, 
8.44691, 10.5337, 3.95141), VARY = c(3.25093, 2.10538, 1.82872, 
3.59716, 5.28257, 2.06862), COVXY = c(-3.42578, -0.952862, -0.31547, 
-3.60645, -5.4154, -0.893447), NBS = c(5L, 5L, 5L, 5L, 5L, 5L
), PENALTY = c(7.7971, 7.81744, 6.65673, 5.41589, 7.83314, 7.67434
), LON = c(35.4497703485906, 35.4497823127734, 35.4497460470858, 
35.4496800644408, 35.4496944443277, 35.4497447585957), LAT = c(32.5439496863703, 
32.5439528535371, 32.5439807906288, 32.5440242771026, 32.5440158397049, 
32.5439918339367), dateTime = structure(c(1593702012.718, 1593702020.718, 
1593702028.717, 1593702036.717, 1593702044.716, 1593702052.716
), tzone = "UTC", class = c("POSIXct", "POSIXt")), date = structure(c(18445, 
18445, 18445, 18445, 18445, 18445), class = "Date"), distance = c(NA, 
1.17743411100137, 4.60468481850777, 7.85311286384701, 1.64317252535601, 
5.42436675576214), dT = c(NA, 8, 7.999, 8, 7.999, 8), spd = c(NA, 
0.147179263875171, 0.57565755950841, 0.981639107980876, 0.205422243449932, 
0.678045844470268), traceNorm = c(3.4756308779846, 2.47746846599508, 
2.19723690120114, 3.47045674227471, 3.97696743763386, 2.45357494281304
), stdVarXY = c(3.6538811068147, 2.68180150040067, 2.40049362707242, 
3.65324436442331, 4.16347652001927, 2.65784450441959), DaNi_Progress = c(187.061048608809, 
187.076647103555, 187.0922455983, 187.107844093045, 187.12344258779, 
187.139041082536), Night = c(1, 1, 1, 1, 1, 1), val1 = c(1.27374914908951, 
1.30912167502659, 1.32253716900396, 1.29467171848917, 1.37470623562867, 
1.30848877011739), val2 = c(3.23381711065944, 2.10334268248794, 
1.75463541415389, 3.21992160484449, 3.73181628241848, 2.07554502203076
), ellipsDir = c(115.424740533603, 112.340304154488, 104.164021881421, 
118.04211471085, 122.067179186874, 111.751545104926), dX = c(NA, 
1.37678413794492, 1.05009921549936, -15.3931077129091, -8.39280412887456, 
0.838136648468208), dY = c(NA, 1.0057812971063, -1.09917296015192, 
13.9633502180222, 5.17642377642915, 2.95164950180333), moveAngle = c(NA, 
53.8507730753772, 136.307991618679, 312.21170751212, 301.665010496707, 
15.8521787470692), projMovStdaxis = c(NA, 0.52265434796425, 0.846713867838062, 
0.969575399403894, 0.999975365742897, 0.102781536221573), Dist = c(NA, 
1.70503096162667, 1.52016103052325, 20.7827552161008, 9.86075677921501, 
3.06833958730147), dt = c(NA, 8, 7.99900007247925, 8, 7.99900007247925, 
8), gap = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), pointInBurst = c(440L, 
440L, 440L, 440L, 440L, 440L), ADP.ID = c(1, 1, 1, 1, 1, 1), 
    ADP.X = c(242497.176034216, 242497.176034216, 242497.176034216, 
    242497.176034216, 242497.176034216, 242497.176034216), ADP.Y = c(716658.678456358, 
    716658.678456358, 716658.678456358, 716658.678456358, 716658.678456358, 
    716658.678456358), ADP.start = c(1593702012718, 1593702012718, 
    1593702012718, 1593702012718, 1593702012718, 1593702012718
    ), ADP.end = c(1593703868598, 1593703868598, 1593703868598, 
    1593703868598, 1593703868598, 1593703868598), ADP.duration = c(1855880, 
    1855880, 1855880, 1855880, 1855880, 1855880), ADP.qlt = c(0.983, 
    0.983, 0.983, 0.983, 0.983, 0.983), seg.ID = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), Ring_ID = c("GG37113", 
    "GG37113", "GG37113", "GG37113", "GG37113", "GG37113"), freq = c(8000, 
    8000, 8000, 8000, 8000, 8000), PointsPernight = c(8396L, 
    8396L, 8396L, 8396L, 8396L, 8396L)), row.names = c(NA, -6L
), class = c("tbl_df", "tbl", "data.frame"))

Screenshot of dput result

I want to print the row(s) when a POSIXct object matches the value in the dateTime attribute. I've tried the following but 0 rows are being returned (while 2020-07-02 15:00:12 is the first element in the dateTime column).

ringTrack[ringTrack$dateTime == as.POSIXct("2020-07-02 15:00:12",tz="UTC"),]

or

ringTrack[ringTrack$dateTime == as.POSIXct("2020-07-02 15:00:12"),]

or

ringTrack[as.POSIXct("2020-07-02 15:00:12") %in% ringTrack$dateTime,]

Using R 4.2.1.


Solution

  • The precision in the date time object is different than the one we wanted to compare

    > format(df$dateTime[1], "%Y%m%d %H%M%OS3")
    [1] "20200702 150012.717"
    > format(as.POSIXct("2020-07-02 15:00:12", tz = "UTC"), "%Y%m%d %H%M%OS3")
    [1] "20200702 150012.000"
    

    Thus, the == failed

    > df$dateTime == as.POSIXct("2020-07-02 15:00:12", tz = "UTC")
    [1] FALSE FALSE FALSE FALSE FALSE FALSE
    

    We may modify the precision to seconds and it should work

    library(lubridate)
    > floor_date(df$dateTime, "sec") == as.POSIXct("2020-07-02 15:00:12", tz = "UTC")
    [1]  TRUE FALSE FALSE FALSE FALSE FALSE