我将 dlply() 与一个自定义函数一起使用,该函数平均 lm() 的斜率适合包含一些 NA 值的数据,并且我收到错误“lm.fit 中的错误(x,y,offset = offset,singular.ok = single.ok, ...) : 0 (non-NA) case"
仅当我使用两个关键变量调用 dlply 时才会发生此错误 - 由一个变量分隔可以正常工作。
令人讨厌的是,我无法使用简单的数据集重现错误,因此我已将问题数据集发布到我的保管箱中。
这是代码,在仍然产生错误的同时尽可能最小化:
masterData <- read.csv("http://dl.dropbox.com/u/48901983/SOquestionData.csv", na.strings="#N/A")
workingData <- data.frame(sample = masterData$sample,
substrate = masterData$substrate,
el1 = masterData$elapsedHr1,
F1 = masterData$r1 - masterData$rK)
#This function is trivial as written; in reality it takes the average of many slopes
meanSlope <- function(df) {
lm1 <- lm(df$F1 ~ df$el1, na.action=na.omit) #changing to na.exclude doesn't help
slope1 <- lm1$coefficients[2]
meanSlope <- mean(c(slope1))
}
lsGOOD <- dlply(workingData, .(sample), meanSlope) #works fine
lsBAD <- dlply(workingData, .(sample, substrate), meanSlope) #throws error
提前感谢您的任何见解。