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