R 日期内部整数存储具有"L" - 可以删除吗?



我有一个返回的 API

str(test)
'data.frame':   35 obs. of  2 variables:
$ date   : Date, format: "2017-05-23" "2017-05-24" "2017-05-25" "2017-05-26" ...
$ PX_LAST: num  52.3 52.1 49.8 50.6 50.5 ...

但是,仔细观察日期变量的内部存储...日期存储时末尾附加一个"L"。

dput(test)
structure(list(date = structure(c(17309L, 17310L, 17311L, 17312L, 
17316L, 17317L, 17318L, 17319L, 17322L, 17323L, 17324L, 17325L, 
17326L, 17329L, 17330L, 17331L, 17332L, 17333L, 17336L, 17337L, 
17338L, 17339L, 17340L, 17343L, 17344L, 17345L, 17346L, 17347L, 
17350L, 17352L, 17353L, 17354L, 17357L, 17358L, 17359L), class = "Date"), 
PX_LAST = c(52.3, 52.09, 49.76, 50.59, 50.48, 49.12, 49.22, 
48.51, 48.22, 48.88, 46.87, 46.85, 46.97, 47.15, 47.45, 45.82, 
45.67, 45.94, 45.46, 44.58, 43.51, 43.74, 44.08, 44.4, 45.31, 
45.81, 46.02, 47.05, 48.01, 46.1, 46.4, 45.07, 45.32, 45.92, 
46.64)), class = "data.frame", .Names = c("date", "PX_LAST"
), row.names = c(NA, 35L))

有没有办法改变日期的存储方式以获得最后的 r=摆脱 L?当我尝试将数据写入 sql 数据库时,额外的 L 会导致错误。

更新

感谢您的评论,里奇,d.b.和马里乌斯。这是我用来写入数据库的 SQL 代码。

好吧,本着试图复制这个非常令人困惑的问题的精神。我已经做到了。下面是产生受限数据类型问题的数据表的一行的结构:

> oneLine <- flatFrame[1, 1-4]
> str(oneLine)
'data.frame':   1 obs. of  4 variables:
$ Ticker  : Factor w/ 1 level "CLU7 Comdty": 1
$ date    : Date, format: "2017-05-18"
$ VOLUME  : num 44674
$ OPEN_INT: int 188049

然后我尝试将这一行写入新表中的数据库,并收到属性冲突错误。

dbWriteTable(con, "new7", oneLine, verbose=TRUE, overwrite=TRUE)
Error in result_insert_dataframe(rs@ptr, values) : 
nanodbc/nanodbc.cpp:1791: 07006: [Microsoft][ODBC Driver 13 for SQL 
Server]Restricted data type attribute violation 

所以现在我尝试克隆数据框:

rep_data <- data.frame(Ticker=as.factor("CLU7 Comdty"), date = as.Date("2017-05-18"), VOLUME=44674, OPEN_INT =as.integer(188049))
> str(rep_data)
'data.frame':   1 obs. of  4 variables:
$ Ticker  : Factor w/ 1 level "CLU7 Comdty": 1
$ date    : Date, format: "2017-05-18"
$ VOLUME  : num 44674
$ OPEN_INT: int 188049

一模一样....但是此写入函数不会产生错误。

dbWriteTable(con, "new8", rep_data, verbose=TRUE, overwrite=TRUE)

这是怎么回事?数据表中是否有一些我没有看到的幻像属性?

github上有人建议我使用dput()命令来查看数据的内部结构。

dput(oneLine)
structure(list(Ticker = structure(1L, .Label = "CLU7 Comdty", class = "factor"), 
date = structure(17304L, class = "Date"), VOLUME = 44674, 
OPEN_INT = 188049L), .Names = c("Ticker", "date", "VOLUME", 
"OPEN_INT"), row.names = 1L, class = "data.frame")
dput(rep_data)
structure(list(Ticker = structure(1L, .Label = "CLU7 Comdty", class = "factor"), 
date = structure(17304, class = "Date"), VOLUME = 44674, 
OPEN_INT = 188049L), .Names = c("Ticker", "date", "VOLUME", 
"OPEN_INT"), row.names = c(NA, -1L), class = "data.frame")

日期结构的显着区别在于,在失败的oneLine中,内部存储的日期17304 L附加了一个">L"。复制的数据集不会。

好吧,似乎该函数需要Date的内部表示为数字而不是整数; 如果是这样,我们只需要将现有整数转换为数字,然后转换为日期。

请注意,问题不在于有一个"L";这只是一个整数输出显示的方式,告诉你它是一个整数,它根本没有在内部使用。因此,除非您的其他函数正在解析dput的输出(非常不可能(,否则问题在于转换为整数,而不是删除 L。

我将首先检查通常的表示形式;它确实使用数字,而不是整数(注意没有L(。

> dput(as.Date("2017-07-01"))
structure(17348, class = "Date")

现在我将制作一个下面有一个整数的版本,它似乎可以用于此目的,但显然不适用于您的目的。

> (foo <- structure(17348L, class="Date"))
[1] "2017-07-01"
> dput(foo)
structure(17348L, class = "Date")

因此,这是将其转换为数字然后转换回日期的方法。R 的起源日期是 1970-01-01,但我没有硬编码,而是将 0 转换为日期。

> (foo2 <- as.Date(as.numeric(foo), origin=structure(0, class="Date")))
[1] "2017-07-01"
> dput(foo2)
structure(17348, class = "Date")

我敢打赌,如果您对日期列执行此操作,它会起作用。

有趣的是,只是重新转换为新日期不会更改为数字。

> dput(as.Date(foo, origin="1970-01-01"))
structure(17348L, class = "Date")

最新更新