【发布时间】:2014-11-04 17:16:26
【问题描述】:
我有一个模型列表,并希望返回它们的系数的 数组(不是列表)。 (出于好奇,我正在对来自一堆不同神经元的数据运行单个模型。我想要一个系数 X 神经元的数组。)如果所有模型都成功运行,则以下工作正常:
Coefs = sapply(ModelList, coef)
但是,如果其中一个模型失败,则 coef() 返回 'NULL',它与其他返回值的长度不同,我最终得到一个列表而不是数组。 :(
我的解决方案是有效的并且是通用的,但是非常笨拙:
Coefs = sapply(ModelList, coef)
typical = Coefs[[1]] # (ought to ensure that this is not NULL!)
typical[1:length(typical)] = NA # Replace all coefficients with NA
Bad = sapply(ModelList, is.null) # Find the bad entries
for (i in which(Bad)) # For each 'NULL', (UGH! A LOOP!)
Coefs[[i]] = typical # replace with a proper entry (of NAs)
Coefs = simplify2array(Coefs) # Now I can convert it to an array
有没有更好的解决方案?
谢谢!
拉里
【问题讨论】: