【问题标题】:Rbindlist error due to string to date conversion in a loop由于循环中的字符串到日期转换导致 Rbindlist 错误
【发布时间】:2018-04-10 12:55:25
【问题描述】:

我正在构建一个循环超过 10-1000 个文件的复杂代码,并根据 6 个分组列为每个文件计算一大堆汇总统计信息。一切正常,但是在双重应用结构中,我还尝试从文件名中提取日期并将其转换为日期格式,并将其作为列添加到每个数据帧中。

在我的完整代码中没有日期转换,以及在这个示例代码中它可以正常工作,但是在其中进行转换时,似乎会导致循环突然产生奇怪的错误。

我已经尝试了十几种方法来让它发挥作用。通常单个字符串到日期格式对我来说不是问题,但是如何在这个循环结构中进行这项工作?

一开始我以为问题是日期格式转换没有用,但是貌似可以用,但是导致rbindlist代码出现问题。

Error in rbindlist(ClusterResultlist[[cl]]) : 
  Column 2 of item 1 is length 11, inconsistent with first column of that item which is length 10. rbind/rbindlist doesn't recycle as it already expects each item to be a uniform list, data.frame or data.table 

我不知道为什么它声称长度不同,或者如何解决它。

问题:如何在循环内部或之后将字符串转换为日期格式。

我的代码:

myfiles <- list("PICO in situ 55 10 100 100 100 2016-05-06 19u03_clustered_newtest1.csv", "PICO in situ 55 10 100 100 100 2016-05-07 19u03_clustered_newtest1.csv")

## list of clustering columns to summarize over
Clusterlist <- c('Cluster_FP1', 'Cl_names_FP1', 'GR_names_FP1', 'Cluster_FP2', 'Cl_names_FP2', 'GR_names_FP2') # 
ClusterResultlist <- vector("list", length(Clusterlist))
names(ClusterResultlist) <- Clusterlist

SummarizeData <- function(y){
  lapply(Clusterlist, function(z) { 
    datetime <- substr(y, nchar(y) -38, nchar(y) -23)
    FullCounts <- data.frame(DummyIndex = 1:10)
    FullCounts$DateTime <- strptime(datetime,format = "%Y-%m-%d %Hu%M")
    ClusterResultlist[[z]][[y]] <<- FullCounts
})}

# run the function over all files
mapply(SummarizeData, y = myfiles)

# create 6 main dataframes out of all sub data frames
lapply(Clusterlist, function(cl) { ClusterResultlist[[cl]] <<- rbindlist(ClusterResultlist[[cl]])  })

更新: 我们现在有两个(部分)解决方案,但它们不会像 rbindlist 那样快,我相信我的实际大数据对象。

我尝试在最终 ClusterResultList 的循环之外进行转换,但这会引发此错误:

lapply(Clusterlist, function(cl) { ClusterResultlist[[cl]] <<- rbindlist(ClusterResultlist[[cl]])  })

lapply(Clusterlist, function(cl) { ClusterResultlist[[cl]]$DateTime <<- strptime(ClusterResultlist[[cl]]$DateTime,format = "%Y-%m-%d %Hu%M") })  



 In `[<-.data.table`(x, j = name, value = value) :
  Supplied 11 items to be assigned to 20 items of column 'DateTime' (recycled leaving remainder of 9 items).

【问题讨论】:

  • P.S.这是我之前写的一个问题的转贴,其中有错误并且不够简化。现在的问题是摘要统计中没有数据框部分,因为我发现该部分与我遇到的问题无关
  • 我尝试使用来自plyrrbind.fill(),它给了我一个时区错误。我回到strptime 调用并添加了tz = "GMT,这对rbind.fill() 起到了作用,但没有解决rbindlist() 的问题。
  • 我猜这是 rbindlist 工作方式的结构性问题。有没有办法在 mapply 之后对列表进行时间转换?
  • 在 SummarizeData 函数中使用 lubridate 似乎可行。 FullCounts$DateTime &lt;- lubridate::ymd_hms(strptime(datetime,format = "%Y-%m-%d %Hu%M"))
  • 如果你把它作为答案发布,我会给你胜利点csgroen!非常好的修复!

标签: r date lapply mapply rbindlist


【解决方案1】:

lubridate 的帮助下修复日期修复了rblindlist 的问题。

替换:

FullCounts$DateTime <- strptime(datetime,format = "%Y-%m-%d %Hu%M")

与:

FullCounts$DateTime <- lubridate::ymd_hms(strptime(datetime,format = "%Y-%m-%d %Hu%M"))

【讨论】:

    【解决方案2】:

    rbind代替rbindlist怎么样?

    lapply(Clusterlist, function(cl) ClusterResultlist[[cl]] <<- do.call(rbind, ClusterResultlist[[cl]]))
    

    【讨论】:

    • 嗯,确实可以。但是我相信我的海量数据结构会慢得多,因此是 rbindlist,我想了解为什么实际会出现问题
    • 如果您将strptime 替换为as.POSIXct,那么rbindlist 也可以工作。所以我真的无法理解你得到的错误背后的原因。顺便说一句,好的部分是你得到了你想要的解决方案。干杯!
    猜你喜欢
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-16
    • 1970-01-01
    • 2018-05-20
    相关资源
    最近更新 更多