从数据帧转换到数据表我得到一个错误的头(..)(converting from data frame to data table I get an error with head(..))

编程入门 行业动态 更新时间:2024-10-25 02:21:37
从数据帧转换到数据表我得到一个错误的头(..)(converting from data frame to data table I get an error with head(..))

下面是一个可重现的示例,显示了该问题:

openSummary <- read.table(textConnection( "Dates dollarA numTotal 7/3/2011 52730.56 1614 7/10/2011 77709.43 1548"), header = TRUE) openSummary$Dates <- strptime(openSummary$Dates,"%m/%d/%Y") str(openSummary) head(openSummary) # No problem openSummaryDT <- data.table(openSummary) str(openSummaryDT) head(openSummaryDT) # An error is produced

这是执行head时的错误(openSummaryDT)

Error in `rownames<-`(`*tmp*`, value = paste(format(rn, right = TRUE), : length of 'dimnames' [1] not equal to array extent

请解释错误,我该如何避免。 但是,似乎我可以对数据帧和数据表进行一些操作,并得到相同的结果。

difftime(Sys.Date(), openSummary[ ,"Dates"]) difftime(Sys.Date(), openSummaryDT[ ,Dates])

先谢谢你

Below is a reproducible example showing the problem:

openSummary <- read.table(textConnection( "Dates dollarA numTotal 7/3/2011 52730.56 1614 7/10/2011 77709.43 1548"), header = TRUE) openSummary$Dates <- strptime(openSummary$Dates,"%m/%d/%Y") str(openSummary) head(openSummary) # No problem openSummaryDT <- data.table(openSummary) str(openSummaryDT) head(openSummaryDT) # An error is produced

Here is the error upon executing head(openSummaryDT)

Error in `rownames<-`(`*tmp*`, value = paste(format(rn, right = TRUE), : length of 'dimnames' [1] not equal to array extent

please explain the error and how can I avoid it. However, it appears that i can do some operation on both data frame and data table and I get the same results.

difftime(Sys.Date(), openSummary[ ,"Dates"]) difftime(Sys.Date(), openSummaryDT[ ,Dates])

Thank you in advance

最满意答案

这是由POSIXlt格式的日期引起的一个引人入胜的错误。 考虑:

openSummary$Dates <- as.Date(openSummary$Dates) head(data.table(openSummary)) # Dates dollarA numTotal # 1: 2011-07-03 52730.56 1614 # 2: 2011-07-10 77709.43 1548

如果您尝试打印原始表,则会出现相同的错误,但是使用回溯,您会看到:

> openSummaryDT # Error in `rownames<-`(`*tmp*`, value = paste(format(rn, right = TRUE), : # length of 'dimnames' [1] not equal to array extent # In addition: Warning message: # In cbind... # Enter a frame number, or 0 to exit # 1: print(list(Dates = list(sec = c(0, 0), min = c(0, 0), hour = c(0, 0), m # 2: print.data.table(list(Dates = list(sec = c(0, 0), min = c(0, 0), hour = # 3: `rownames<-`(`*tmp*`, value = paste(format(rn, right = TRUE), ":", sep > 3 # Called from: print.data.table(list(Dates = list(sec Browse[1]> ls() # [1] "dn" "value" "x" Browse[1]> x # Dates dollarA numTotal # sec "0,0" "52730.56" "1614" # min "0,0" "77709.43" "1548" # hour "0,0" "52730.56" "1614" # mday "3,10" "77709.43" "1548" # mon "6,6" "52730.56" "1614" # year "111,111" "77709.43" "1548" # wday "0,0" "52730.56" "1614" # yday "183,190" "77709.43" "1548" # isdst "1,1" "52730.56" "1614"

基本上,无论出于何种原因,将data.table转换为打印/标题的文本形式的过程都暴露了POSIXlt对象的基础列表/向量性质。

This is a fascinating bug caused by the dates being in POSIXlt format. Consider:

openSummary$Dates <- as.Date(openSummary$Dates) head(data.table(openSummary)) # Dates dollarA numTotal # 1: 2011-07-03 52730.56 1614 # 2: 2011-07-10 77709.43 1548

If you try to print the original table, you get the same error, but with a traceback, you see this:

> openSummaryDT # Error in `rownames<-`(`*tmp*`, value = paste(format(rn, right = TRUE), : # length of 'dimnames' [1] not equal to array extent # In addition: Warning message: # In cbind... # Enter a frame number, or 0 to exit # 1: print(list(Dates = list(sec = c(0, 0), min = c(0, 0), hour = c(0, 0), m # 2: print.data.table(list(Dates = list(sec = c(0, 0), min = c(0, 0), hour = # 3: `rownames<-`(`*tmp*`, value = paste(format(rn, right = TRUE), ":", sep > 3 # Called from: print.data.table(list(Dates = list(sec Browse[1]> ls() # [1] "dn" "value" "x" Browse[1]> x # Dates dollarA numTotal # sec "0,0" "52730.56" "1614" # min "0,0" "77709.43" "1548" # hour "0,0" "52730.56" "1614" # mday "3,10" "77709.43" "1548" # mon "6,6" "52730.56" "1614" # year "111,111" "77709.43" "1548" # wday "0,0" "52730.56" "1614" # yday "183,190" "77709.43" "1548" # isdst "1,1" "52730.56" "1614"

Basically, for whatever reason, the process of converting data.table into text form for printing / heading exposes the underlying list/vector nature of the POSIXlt object.

更多推荐

本文发布于:2023-07-29 20:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319582.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据表   错误   数据   converting   data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!