Chriscross Chriscross - 3 months ago 37
R Question

How to Fix "Variable Lengths Differ" Error in r When Doing a Linear Regression

I'm trying to make a for loop that loops through my data frame and runs a linear regression of all columns against

AcquisitionTime
which is a double. All columns are of the same length. Eventually I want to just use one of the apply functions instead of the for loop to do this but I am really new at r and the apply functions don't make sense to me.

##pdf("ROIs_vs_AcquisitionTime_lm.pdf")
x = 0
for (i in names(raw_data)){
if(x > 9){
fit <- lm(i ~ AcquisitionTimes, data = raw_data)
##print(ggplotRegression(fit))
}
x <- x + 1
}
##dev.off()


When I run this code I get this error:

Error in model.frame.default(formula = i ~ AcquisitionTimes, data = raw_data,:
variable lengths differ (found for 'AcquisitionTimes')


What is causing this error? I checked to see if I had any other data frames named raw_data, but I did not. I even cleared the objects in R to see if that would help. Everywhere I read says that this is either because the variable
AcquisitionTimes
does not exist in the data frame or because there are identical variable names somewhere in my data. So far I haven't found any.

jav jav
Answer

I have mentioned the fix already in my comment but the following simple example can reproduce your error:

data(iris)
i = "Sepal.Length"
lm(i ~ Sepal.Width, data = iris)

You will see

Error in model.frame.default(formula = i ~ Sepal.Width, data = iris, drop.unused.levels = TRUE) : 
variable lengths differ (found for 'Sepal.Width')

To correct this, we can instead do:

lm(paste(i , "~ Sepal.Width"), data = iris)

which works as expected:

Call:
lm(formula = paste(i, "~ Sepal.Width"), data = iris)

Coefficients:
(Intercept)  Sepal.Width  
     6.5262      -0.2234

In your case, it will be

lm(paste(i, "~ AcquisitionTimes"), data = raw_data)
Comments