how to solve error replacement has 0 rows, data has 200 for a loop

时间:2018-04-20 00:58:06

标签: r loops dataframe

I have 3 df which I put them in a list. For each df, I adjust values in a zvar function. Then I want to run a model and then resampling for the model in a loop applying the zvar.

But I encounter this error:

Error in $<-.data.frame(*tmp*, "var2", value = numeric(0)) : replacement has 0 rows, data has 200

The message says that a variable does not exist, but I am able to print each variable in the dataframe, what is the issue here?

My df sample (all continuous vars):

  var1     var2
   2        2
   1        2
   9        7

My code:

zvar <- function(data, mean, sd){
     data$var2 <- (data$var2 - mean$var2)/sd$var2
     data$var1 <- (data$var1 - mean$var1)/sd$var1
     return(data)
 }
 # its args are automatically calculated in the following function `boot.fiml`.


dflist <- list(group1, group2, group3)

for (i in dflist) {
   i <- data.frame(i)
   print(i$var2)
   print(i$var1)
   m_group <- lm(var1 ~ var2, data = i)
   print(m_group)

   ## previous lines run successfully, not not the following one
   boot.fiml(data = i, model = m_group, R = 10, z.function = zvar)
 }

Thanks!

0 个答案:

没有答案