Chriscross Chriscross - 1 year ago 340
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

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.

x = 0
for (i in names(raw_data)){
if(x > 9){
fit <- lm(i ~ AcquisitionTimes, data = raw_data)
x <- x + 1

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
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 Source

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

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:

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

(Intercept)  Sepal.Width  
     6.5262      -0.2234

In your case, it will be

lm(paste(i, "~ AcquisitionTimes"), data = raw_data)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download