0

情况

我有一个运动员数据框

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")

的格式birthdateDate

> 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" ...

我的理解是 mongodb 使用 UTC 日期格式,所以我birthdate使用as.POSIXct

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

然后我把它变成一个列表,以便创建一个 bson 对象batch.insert到一个 mongodb 数据库(根据这个问题

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))})

其中给出(第一项):

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

问题

然后我检查birthdate值是否已正确转换

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

但它正在显示1984-02-28,而不是预期的2004-11-09

query为什么我的步骤中生成的 UTC 日期与实际变量之间存在差异birthdate

笔记

我试过按照这里的建议as.POSIXct调用字符串,但得到了相同的结果。birthdate

编辑

> 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 

编辑 2 - 对@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
4

0 回答 0