场合
我有一个运动员数据框
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")
birthdate
的格式为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" ...
我的理解是mongodb使用UTC日期格式,因此我使用birthdate
转换as.POSIXct
变量
df_ath$birthdate <- as.POSIXct(df_ath$birthdate, tz="GMT")
然后我将其转换为一个列表,以便创建一个bson对象到batch.insert
到一个mongodb数据库(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))})
给出(第一项):
> 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
as suggested here的字符串上调用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