Home > Software engineering >  Getting error of invalid type for list while my object is a dataframe
Getting error of invalid type for list while my object is a dataframe

Time:03-04

I have the dataframe below:

df<-structure(list(Answer.circleAnswer = c(221, 703, 706, 551, 359, 
473, 709, 780, 736, 608, 211, 291, 483, 394, 768, 610, 679, 799, 
331, 484, 224, 370, 767, 355, 419), Answer.circlesGuessSlider = c(499, 
550, 800, 651, 700, 650, 611, 800, 567, 500, 400, 200, 305, 550, 
800, 271, 650, 500, 335, 600, 200, 350, 650, 359, 530), Answer.circlesGuessText = c(499, 
550, 1000, 651, 700, 650, 611, NA, 567, 500, 400, 200, 305, 550, 
NA, 271, 650, 500, 335, 600, 50, 350, 650, 359, 530)), row.names = c(NA, 
-25L), class = c("tbl_df", "tbl", "data.frame"))

and Im trying to:

vif.1 <- usdm::vif(df)

but I get:

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

CodePudding user response:

When you explore of the structure of the object df using str(df) , the result is tibble.

When you update the code to remove the tbl option, the explore the structure of the object with str(df), the results is data.frame. This allows you to run the vif() without error

df<-structure(list(Answer.circleAnswer = c(221, 703, 706, 551, 359, 
                                           473, 709, 780, 736, 608, 211, 291, 483, 394, 768, 610, 679, 799, 
                                           331, 484, 224, 370, 767, 355, 419), Answer.circlesGuessSlider = c(499, 
                                                                                                             550, 800, 651, 700, 650, 611, 800, 567, 500, 400, 200, 305, 550, 
                                                                                                             800, 271, 650, 500, 335, 600, 200, 350, 650, 359, 530), Answer.circlesGuessText = c(499, 
                                                                                                                                                                                                 550, 1000, 651, 700, 650, 611, NA, 567, 500, 400, 200, 305, 550, 
                                                                                                                                                                                                 NA, 271, 650, 500, 335, 600, 50, 350, 650, 359, 530)), row.names = c(NA, 
                                                                                                                                                                                                                                                                      -25L), class = c("data.frame"))
str(df)
vif.1 <- usdm::vif(df)

console output

CodePudding user response:

Convert to data.frame or matrix and it should work

 usdm::vif(as.data.frame(df))
                  Variables       VIF
1       Answer.circleAnswer  1.408335
2 Answer.circlesGuessSlider 19.887190
3   Answer.circlesGuessText 20.169362

because according to ?vif

x - explanatory variables (predictors), defined as a raster object (RasterStack or RasterBrick), or as a matrix, or as a data.frame.

Here, the object is not only a data.frame, but also a tibble (tbl_df).

> class(df)
[1] "tbl_df"     "tbl"        "data.frame"

Therefore, it showed the error

> usdm::vif(df)
Error in model.frame.default(formula = y[, i] ~ ., data = y[-i], drop.unused.levels = TRUE) : 
  invalid type (list) for variable 'y[, i]'
  •  Tags:  
  • r
  • Related