【问题标题】:Combining dplyr::mutate with lubridate::ymd_hms in R randomly causes segfault在 R 中将 dplyr::mutate 与 lubridate::ymd_hms 随机组合会导致段错误
【发布时间】:2015-09-09 17:17:24
【问题描述】:

我已尽我所能搜索与此相关的内容,但在 SO 或 dplyr github 上没有找到任何内容;可能是一个新问题,因为下面的代码在今天之前运行良好?

这个问题在概念上很简单:有时调用my_data %>% mutate(x = ymd_hms(x)),但并不总是(即随机),会导致 R 因捕获的段错误而崩溃。我已将问题简化为最简单的形式(也在这里:https://gist.github.com/john-sandall/05c3abb24fc738ddc2ad):

require(lubridate)
require(dplyr)

set.seed(42)
make_some_random_datetimes = function(n) ymd("2015-01-01") + seconds(runif(n, min=0, max=60*60*24*365))

d = data.frame(
  col1 = make_some_random_datetimes(5000),
  col2 = make_some_random_datetimes(5000)
)

do_it = function() {
  d %>% mutate(
    col1 = ymd_hms(col1),
    col2 = ymd_hms(col2)  # for some reason, it only crashes when evaluating 2+ cols, if we removed this line it'd be fine
  )
  return(TRUE)
}

do_it()  # doesn't crash every time...it fails every nth time where n is randomly distributed with mean of roughly 7.7

do_it_lots_of_times = function(n) for (i in 1:n) do_it()

do_it_lots_of_times(50)  # almost guaranteed to fail on my machine

所以在某些时候,上面运行do_it() 会导致段错误,在终端中运行 R 的输出是

*** caught segfault ***
address 0x0, cause 'unknown'

我今天早上升级到 R 版本 3.2.1,虽然回滚到 3.2.0 并重新安装库没有帮助。然后我尝试卸载/重新安装 R(使用 brew install r 和完全更新/升级的自制软件),然后重新安装上面所有必需的软件包。这是 sessionInfo() 的输出:

R version 3.2.1 (2015-06-18)
Platform: x86_64-apple-darwin14.3.0 (64-bit)
Running under: OS X 10.10.3 (Yosemite)

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_0.4.2     lubridate_1.3.3

loaded via a namespace (and not attached):
[1] lazyeval_0.1.10 R6_2.0.1        assertthat_0.1  magrittr_1.5    plyr_1.8.3      parallel_3.2.1 
[7] DBI_0.3.1       tools_3.2.1     memoise_0.2.1   Rcpp_0.11.6     stringi_0.5-2   digest_0.6.8   
[13] stringr_1.0.0  

作为一名统计学家,我的想法已经不多了,我决定查看失败率的分布,看看这是否有助于阐明一些问题。如果在上面运行do_it() 第 n 次崩溃,并且我写下了 n 落在 50 次崩溃的位置(例如,第 3 次尝试,然后是第 7 次尝试),我得到这个序列:

3, 7, 9, 20, 9, 9, 9, 7, 4, 23, 6, 3, 3, 3, 7, 7, 3, 9, 6, 6, 7, 10, 13, 7, 3, 7, 4, 7, 9, 6, 7, 7, 6, 6, 7, 7, 7, 9, 6, 12, 7, 7, 5, 9, 18, 6, 7, 9, 9, 7

这给了我这个分布:

我不知道这是否相关或有帮助,尽管我注意到的另一件事是将数据帧 d 中的行数从 5000 增加到 10000 似乎将 n 的平均值从 ~8 增加到 ~ 20.

非常欢迎对此提供任何帮助!

【问题讨论】:

  • 很可能与此有关:github.com/hadley/dplyr/issues/1231
  • 在 windows 上确认,最新的 64bit R,不是最新的 rstudio 版本。 dplyr_0.4.2 lubridate_1.3.3 崩溃每次奇怪的是没有写入 rstudio 日志文件

标签: r segmentation-fault dplyr lubridate


【解决方案1】:

90% 确定这是最新版本 dplyr (0.4.2) 中的错误,请在此处查看此问题:https://github.com/hadley/dplyr/issues/1231

将我的 dplyr 版本降级到 0.4.1 可以解决问题:

packageurl = "http://cran.r-project.org/src/contrib/Archive/dplyr/dplyr_0.4.1.tar.gz"
install.packages(packageurl, repos=NULL, type="source", dependencies = TRUE)

【讨论】:

  • 对我来说不会崩溃,n = 1000, RStudio 0.98.1062. dplyr_0.4.0, lubridate_1.3.3 0.4.2 听起来像问题。
  • 不会崩溃,n=1000,dplyr_0.4.1
猜你喜欢
  • 2015-08-25
  • 1970-01-01
  • 1970-01-01
  • 2019-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多