'R Error message: invalid type (list) for variable

Basically, I've made linear regressions with my dataset and now I am having to also add a new dataset so I can add an extra variable. I want to answer another one of my research hypothesis. I think I'm not adding the dataset in properly or something because this is the error code I am getting: Error in model.frame.default(formula = mh_w3 ~ malaise_50 + CW3_FRTVEGSP + : invalid type (list) for variable 'malaise_50'

This is the code used: malaise_50<- read.csv(file.choose()) malaise_50<- unlist(malaise_50)

malaise_50<-na.omit(malaise_50)

lm_3 <-lm(mh_w3~ CW3_FRTVEGSP+malaise_50+CW3_FINANCIALMAND+n622,data = data)

All the variables for the linear regression have already been made, the linear regression has been ran without the new variable (malaise_50) and works, but it doesn't run with the new variable. Can anyone help me figure out what I'm doing wrong?

The image shows the data I am using in the malaise_50 variable



Solution 1:[1]

You only removed NA observations in the malaise_50 dataset, not in the data dataset.

Assuming your new dataset malaise_50 is of the same number of rows (observations) as data. You will need to first merge it into data, then omit all observations with NA's in data, then run linear regression on data:

data <- cbind(data, malaise_50) # merge
data <- na.omit(data) # remove NA
lm(..., data = data) # lm

The merging part might be different if you have a variable as the matching IDs.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zhili Qiao