I'm using dlply() with a custom function that averages slopes of lm() fits on data that contain some NA values, and I get the error "Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases"
This error only happens when I call dlply with two key variables - separating by one variable works fine.
Annoyingly I can't reproduce the error with a simple dataset, so I've posted the problem dataset in my dropbox.
Here's the code, as minimized as possible while still producing an error:
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
Thanks in advance for any insight.