0

Situation

I have a data frame of athletes

df_ath <- structure(list(athlete = c("ath_1", "ath_2", "ath_3", "ath_4", 
    "ath_5"), country = c("AU", "AU", "AU", "AU", "AU"), birthdate = structure(c(12731, 
    13056, 11964, 12678, 13086), class = "Date")), .Names = c("athlete", 
    "country", "birthdate"), row.names = c(NA, 5L), class = "data.frame")

where the format of birthdate is Date

> str(df_ath)
'data.frame':   5 obs. of  3 variables:
 $ athlete  : chr  "ath_1" "ath_2" "ath_3" "ath_4" ...
 $ country  : chr  "AU" "AU" "AU" "AU" ...
 $ birthdate: Date, format: "2004-11-09" "2005-09-30" ...

It's my understanding that mongodb uses UTC date formats, so I'm converting the birthdate variable using as.POSIXct

df_ath$birthdate <- as.POSIXct(df_ath$birthdate, tz="GMT")

I am then turning this into a list in order to create a bson object to batch.insert into a mongodb database (as per this question)

library(rmongodb)
lst_ath <- Map(Filter, list(Negate(is.na)), split(df_ath, row(df_ath)))
query <- lapply(lst_ath, function(x){mongo.bson.from.list(as.list(x))})

Which gives (the first item):

> query
[[1]]
    athlete : 2      ath_1
    country : 2      AU
    birthdate : 9    446772224

Issue

I then check the birthdate value has been converted correctly

> format(as.POSIXct(446772224, origin="1970-01-01"), format="%Y-%m-%d")
[1] "1984-02-28"

But it's showing 1984-02-28, and not the expected 2004-11-09.

Why is there a difference between the UTC date generated in my query step, and the actual birthdate variable?

Notes

I've tried calling as.POSIXct on strings of birthdate as suggested here but got the same outcome.

Edit

> sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: i386-w64-mingw32/i386 (32-bit)

locale:
[1]  LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rmongodb_1.8.0

loaded via a namespace (and not attached):
[1] jsonlite_0.9.10 plyr_1.8.1      Rcpp_0.11.2     tools_3.1.2 

Edit 2 - response to @nicola

> as.numeric(df_ath$birthdate)
[1] 1099958400 1128038400 1033689600 1095379200 1130630400
> mongo.bson.from.df(df_ath)
[[1]]
    athlete : 2      ath_1
    country : 2      AU
    birthdate : 9    446772224

[[2]]
    athlete : 2      ath_2
    country : 2      AU
    birthdate : 9    -1537998848
Community
  • 1
  • 1
tospig
  • 7,762
  • 14
  • 40
  • 79
  • @nicola - a typo - it should be `df_ath` - I copied the wrong line. I've updated the question – tospig Feb 21 '15 at 07:37
  • Can't reproduce. I get `1099958400000` which is the correct value for the first date (if expressed in milliseconds). – nicola Feb 21 '15 at 07:46
  • @nicola That's odd. I've just restarted my session, cleared the environment and ran just the code given here in the question and still get the same date values. I've added `sessionInfo()` now too. – tospig Feb 21 '15 at 07:51
  • I installed `rmongodb` right now. Maybe try to reinstall it Edit: i saw your edit and we have the same version. Unlikely this is the issue. By the way, I'm on a Linux system. – nicola Feb 21 '15 at 07:53
  • @nicola just removed and re-installed rmongodb - no change in my answers. – tospig Feb 21 '15 at 07:56
  • Are date values converted correctly? What you have if you try `as.numeric(df_ath$birthdate)`? What happens if you execute directly `mongo.bson.from.df(df_ath)` (which works for me)? – nicola Feb 21 '15 at 08:00
  • @nicola I've added a second edit to show you outcome of both commands. – tospig Feb 21 '15 at 08:07
  • Should be a bug. I guess you should mail to `maintainer("rmongodb")`. – nicola Feb 21 '15 at 08:16
  • I've sent the maintainer an email. I'll update this post as and when I get a resolution. Thanks for your comments @nicola . – tospig Feb 21 '15 at 08:57
  • Can anybody else reproduce this? I ended with `[[1]] athlete : 2 ath_1 country : 2 AU birthdate : 9 1099958400000` – Dmitriy Selivanov Feb 23 '15 at 16:17

0 Answers0