R 日期内部整数存储有 "L" - 可以删除吗?
R Date Internal Integer Storage Has "L" - Can it be removed?
我有一个 API returns
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=rid 结尾的 L?当我尝试将数据写入 sql 数据库时,那个额外的 L 导致了错误。
更新
感谢您的评论,丰富,d.b。和马吕斯。这是我用来写入数据库的 SQL 代码。
好的,本着尝试复制这个非常令人困惑的问题的精神。我已经做到了。这是产生受限数据类型问题的一行数据 table 的结构:
> 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
然后我尝试将这一行写入新的数据库 table,但我收到了属性违规错误。
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)
这是怎么回事?数据 table 中是否有一些我没有看到的幻影属性?
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")
我有一个 API returns
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=rid 结尾的 L?当我尝试将数据写入 sql 数据库时,那个额外的 L 导致了错误。
更新
感谢您的评论,丰富,d.b。和马吕斯。这是我用来写入数据库的 SQL 代码。
好的,本着尝试复制这个非常令人困惑的问题的精神。我已经做到了。这是产生受限数据类型问题的一行数据 table 的结构:
> 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
然后我尝试将这一行写入新的数据库 table,但我收到了属性违规错误。
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)
这是怎么回事?数据 table 中是否有一些我没有看到的幻影属性?
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")