Search code examples
rdataframerasterscatter-plotlm

How to solve Error in (function (formula, data = NULL, subset = NULL, na.action = na.fail, : invalid type (list) for variable 'y' in R?


I have two dataframes.

 > head(x)
VegCX2X0.7
1          0
2          0
3          0
4          0
5          0
6          0

> head(y)
  layer
1     0
2     0
3     0
4     0
5     0
6     0

The code above shows only 6 observations for each dataframe, however there are 1000 plus observations. I would like to compute a scatter plot between these two dataframes. The NA values are set to 0. I try this code:

mydata = data.frame(x,y)
fit <- lm(y~x, data = mydata)

The error which is produced is

Error in model.frame.default(formula = y ~ x, data = mydata, drop.unused.levels = TRUE) :    invalid type (list) for variable 'y'

I really don't understand how can I solve this error.


Solution

  • Your column names are not y and x, those are the names of your data.frames. If you want to keep the mydata = data.frame(x,y) structure follow it up with:

    fit <- lm(layer~VegCX2X0.7, data = mydata)
    

    if you prefer keeping it to seperate data.frames you can do this as well

    fit <- lm(y$layer~x$VegCX2X0.7)