r - Why this list can't be stored as element in data frame? -


i ran randomforest model , tried store model element in data frame. wrap things list() , store element, here seems need 2 layers list(list()). can explian why, , tell me if list(list()) way fix this?

library(randomforest)  data1 = data.frame(a = sample.int(100, size = 100)) data1$b = data1$a data1$c = data1$a data1$d = data1$a  report = data.frame(ntree = 500, mtry = 1:3, model = na)  ( i_row in 1:nrow(report)){          ntree = report[i_row, 'ntree']         mtry = report[i_row, 'mtry']          rf = randomforest( d ~ ., data = data1, importance = t, ntree = ntree, mtry = mtry)          report[i_row, 'model'] = rf  # not work         report[i_row, 'model'] = list(rf)  # not work         report[i_row, 'model'] = list(list(rf))  # works } 

data frames internally lists , if consider str(rf) see randomforest-model internally represented list. attributes have different dimensions, rf can't transformed data.frame

r tries best convert list or list of lists in reasonable way data.frame. consider

a <- data.frame(x=c(1,2),y=c(1,2)) 

in assignment

a[2,] <- list(x=3, y=3) 

the right hand list interpreted row assigned second row of a.

the assignment a[2,] <- list(list(x=3, y=3)) fails because right hand side cannot interpreted row, can coerced column:

a[,1] <- list(list(x=3, y=3)) 

this results in

  x y 1 3 1 2 3 2 

finally, list(list(...)) "trick" in case is:

a[2,] <- list(list(list(x=3, y=3))) >      x    y 1    1    1 2 3, 3 3, 3 

now r gave coerce right hand side object rows , columns , accepted wrapped list of lists. more or less same did.

so @ least works reproducible. if idea? deny it.

dataframes intended tabular data, not wrapping complex objects lists of lists.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -