Error in dataframe *tmp* replacement has x data has y

放肆的年华 提交于 2019-12-18 07:44:12

问题


I'm a beginner in R. Here is a very simple code where I'm trying to save the residual term:

# Create variables for child's EA:

dat$cldeacdi <- rowMeans(dat[,c('cdcresp', 'cdcinv')],na.rm=T)
dat$cldeacu <- rowMeans(dat[,c('cucresp', 'cucinv')],na.rm=T)

# Create a residual score for child EA:

dat$cldearesid <- resid(lm(cldeacu ~ cldeacdi, data = dat))

I'm getting the following message:

Error in `$<-.data.frame`(`*tmp*`, cldearesid, value = c(-0.18608488908881,  : 
  replacement has 366 rows, data has 367

I searched for this error but couldn't find anything that could resolve this. Additionally, I've created the exact same code for mom's EA, and it saved the residual just fine, with no errors. I'd be grateful if someone could help me resolve this.


回答1:


I have a feeling you have NAs in your data. Look at this example:

#mtcars data set
test <- mtcars
#adding just one NA in the cyl column
test[2, 2] <- NA

#running linear model and adding the residuals to the data.frame
test$residuals <- resid(lm(mpg ~ cyl, test))
Error in `$<-.data.frame`(`*tmp*`, "residuals", value = c(0.382245430809409,  : 
  replacement has 31 rows, data has 32

As you can see this results in a similar error to yours.

As a validation:

length(resid(lm(mpg ~ cyl, test)))
#31
nrow(test)
#32

This happens because lm will run na.omit on the data set prior to running the regression, so if you have any rows with NA these will get eliminated resulting in fewer results.

If you run na.omit on your dat data set (i.e. dat <- na.omit(dat) at the very beginning of your code then your code should work.




回答2:


This is an old thread, but maybe this can help someone else facing the same issue. To LyzandeR's point, check for NA's as a first line of defense. In addition, make sure that you don't have any factors in x, as this can also cause the error.



来源:https://stackoverflow.com/questions/47232728/error-in-dataframe-tmp-replacement-has-x-data-has-y

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!