Search code examples
rlistdataframer-caret

Error when changing a list to a data frame


I am trying to save the results of the caret package function varImp() (it is called caretVarImp) as a data frame. I get the following error when writing saving to excel: Error in write_xlsx(caretVarImp, "C:/Users/xx/Desktop/xxx/data/caretVarImp.xlsx") : Argument x must be a data frame or list of data frames

This is the code I have tried to coerce the list to a dataframe (none work with the two columns I need the variable name as column 1 and the importance as column 2)

as.data.frame(caretVarImp[1])
as.data.frame(caretVarImp)
do.call(rbind.data.frame, caretVarImp)
do.call(rbind.data.frame, caretVarImp) 

Here is a sample of my data:

structure(list(importance = structure(list(Overall = c(73.0652774030728, 
5.59788533193309, 12.0493266941609, 10.3386457708192, 9.57393450408533, 
5.20541566722316, 1.3288171116571, 6.35796455170938, 11.0935135074297, 
11.1559016120386, 2.40781736204481, 0.703213050653965, 11.5347692336125, 
0.937420219353987, 4.47109480179074, 0.0470156705635818, 0.158416217418262, 
1.35381531327241, 6.2701821289417, 2.65864656303475, 1.69685872584132, 
2.2675644307254, 1.62780296736856, 0.00855219101468707, 1.87661286537946, 
1.09727044098617, 2.74041675134488, 1.23786457706451, 8.83041668981274, 
2.4225639811227, 0.286604208712965, 1.52949037270653, 6.03677040957175, 
4.15204475304192, 1.09846208156958, 1.61659364294715, 1.76557530305865, 
0, 2.01967480498812, 1.08329243876112, 2.84187833461568, 10.9084292761061, 
0.915296886162838, 2.69586735645909, 2.3910000882193, 1.33643391850196, 
7.56312544985906, 1.62542508956097, 1.47449134664207, 1.03027425996789, 
1.98025126217767, 11.6845488515631, 0.0893795415248947, 2.21514765028306, 
2.01986607764691, 1.29315826619842, 6.26319859009358, 3.05821732792535, 
1.70703118435575, 0.767842346252973, 1.50569212601442, 12.8850643837551, 
0.127850722802544, 1.73664878251376, 1.06397923302445, 0.341640676222888, 
6.89008391732415, 3.90084252998416, 1.61479430802966, 0.287676328419298, 
2.09035574634407, 80.3198239777205, 80.4142898640079, 86.2331430125215, 
86.9372667058436, 100, 97.6371166550049, 91.5186553702695, 94.1976827669785, 
74.1410992786718, 73.1080835238438, 75.1894759938387, 80.1260776091768
)), class = "data.frame", row.names = c("Age", "Drainage1", "Drainage2", 
"Drainage3", "Drainage4", "Drainage5", "Drainage6", "HSG2", "HSG3", 
"HSG4", "HSG5", "HSG6", "HSG7", "LU20052", "LU20053", "LU20054", 
"LU20055", "LU20056", "LU20057", "LU20058", "LU20059", "LU200510", 
"LU200511", "LU200512", "LU200513", "LU200514", "LU200515", "LU19902", 
"LU19903", "LU19904", "LU19905", "LU19906", "LU19907", "LU19908", 
"LU19909", "LU199010", "LU199011", "LU199012", "LU199013", "LU199014", 
"LU199015", "LU19752", "LU19756", "LU19757", "LU19758", "LU19759", 
"LU197510", "LU197511", "LU197513", "LU197514", "LU197515", "LU19602", 
"LU19606", "LU19607", "LU19608", "LU19609", "LU196010", "LU196011", 
"LU196013", "LU196014", "LU196015", "LU19452", "LU19456", "LU19457", 
"LU19458", "LU19459", "LU194510", "LU194511", "LU194513", "LU194514", 
"LU194515", "NO3_180", "NO3_400", "DO5", "DO2", "Precip2005", 
"Precip1990", "Precip1975", "Precip1960", "Irrig2005", "Irrig1990", 
"Irrig1975", "Irrig1960")), model = "rf", calledFrom = "varImp"), class = "varImp.train")

Solution

  • If we need the rownames as a column

    library(dplyr)
    library(tibble)
    caretVarImp$importance %>% 
       rownames_to_column('variable') %>%
       as_tibble()
    

    -output

    # A tibble: 83 × 2
       variable  Overall
       <chr>       <dbl>
     1 Age         73.1 
     2 Drainage1    5.60
     3 Drainage2   12.0 
     4 Drainage3   10.3 
     5 Drainage4    9.57
     6 Drainage5    5.21
     7 Drainage6    1.33
     8 HSG2         6.36
     9 HSG3        11.1 
    10 HSG4        11.2 
    # … with 73 more rows