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

标签 r date structure

我有一个返回的 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 中,内部存储的日期 17304L 附加了一个“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")

关于R日期内部整数存储有 "L"- 可以删除吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45046282/

相关文章:

r - 为什么 names(x) 比 attr(x, "names") 好?

在R中将数据从长 reshape 为半宽

r - 如何在随机数据集上绘制条件推理树?

date - 时间提取(即从自由格式文本中提取日期/时间实体) - 如何?

c - 空格分隔的文件到结构数组 C

PHP 应用程序结构/模式 - 2 个具有共享库和 Assets 的站点

sql - RODBC:执行包含多个语句的查询

linux - 如何在linux shell中将4月1日转换为3月31日

php - 如何区分日期列表之间的日期? PHP

c - 结构本身的地址