使用带有segfault错误的汇总时,dplyr崩溃

时间:2015-08-10 09:04:30

标签: r dplyr

我的 @see #TabLayout_tabBackground @see #TabLayout_tabContentStart @see #TabLayout_tabGravity @see #TabLayout_tabIndicatorColor @see #TabLayout_tabIndicatorHeight @see #TabLayout_tabMaxWidth @see #TabLayout_tabMinWidth @see #TabLayout_tabMode @see #TabLayout_tabPadding @see #TabLayout_tabPaddingBottom @see #TabLayout_tabPaddingEnd @see #TabLayout_tabPaddingStart @see #TabLayout_tabPaddingTop @see #TabLayout_tabSelectedTextColor @see #TabLayout_tabTextAppearance @see #TabLayout_tabTextColor 脚本有时会在此代码段中崩溃:

dplyr

崩溃错误是:

abc.fit <- abc_bySubject %>%
  do(fit = lm(value ~ delta, .)) %>%
  summarise(fvc_intercept = coef(fit)[1],
        fvc_slope = coef(fit)[2])

但是,当我在Rstudio中执行此部分时出现错误 *** caught segfault *** address 0x7ff041000098, cause 'memory not mapped' ,但频率较低,也会发生这种情况。当我在R命令行中获取脚本时,总会发生这种情况。 我在具有大量RAM的不同机器上测试了它。 R和所有软件包都是uptodate,我使用的是最新版本的Ubuntu。

这可能与此问题有关:link但它说这是固定的。

也许有一个更好的解决方案

1 个答案:

答案 0 :(得分:2)

不使用summarise(OP的代码在dplyr_0.4.1.9000中工作)以获得预期输出的另一个选项是从coef中提取lm,将其转换为{{ 1}},更改列表元素的“名称”(list)并在setNames环境中转换回data.frame

do

如果我们需要删除“主题”列,我们可以library(dplyr) abc.fit <- abc_bySubject %>% do(data.frame(setNames(as.list(coef(lm(value~delta, data=.))), c('fvc_intercept','fvc_slope' )))) abc.fit # Subject fvc_intercept fvc_slope #1 1 0.5319503 -0.03147698 #2 2 0.4478791 0.04293860 #3 3 0.4318059 -0.03276570 并使用ungroup()选择“主题”以外的列

select

另一种选择是abc.fit %>% ungroup() %>% select(-Subject) # fvc_intercept fvc_slope #1 0.5319503 -0.03147698 #2 0.4478791 0.04293860 #3 0.4318059 -0.03276570 。我们将'data.frame'转换为'data.table'(data.table),按'主题'列分组,我们得到setDT(abc)的系数(coef),转换为lmlist)并设置列的名称(as.list)。

setnames

我们可以从'res'

对感兴趣的列进行子集化
 library(data.table)
 res <- setnames(setDT(abc)[, as.list(coef(lm(value~delta))),
               by =Subject],2:3, c('fvc_intercept', 'fvc_slope'))[]
 res
 #   Subject fvc_intercept   fvc_slope
 #1:       1     0.5319503 -0.03147698
 #2:       2     0.4478791  0.04293860
 #3:       3     0.4318059 -0.03276570

数据

res[,-1, with=FALSE]
#   fvc_intercept   fvc_slope
#1:     0.5319503 -0.03147698
#2:     0.4478791  0.04293860
#3:     0.4318059 -0.03276570