我有这段代码,用于创建一个数据框,用于保存多个分类模型的性能矩阵:
eval_table <- data.frame(norm_method_1=c("","","","",""),norm_method_2=c("","","","",""),norm_method_3=c("","","","",""),norm_method_4=c("","","","",""),norm_method_5=c("","","","",""),norm_method_6=c("","","","",""),norm_method_7=c("","","","",""),norm_method_8=c("","","","",""))
rownames(eval_table) <- c("Log. Model Coeff","Log. Model Confusion Matrix","Log. Model AUC","Linear Modl Coeff","Linear Model overfit ratio")
col <- list(mnm_coef,cm,auc,lm_summary$coefficients,lm_summary$training_testing_error_ratio)
eval_table[,c("norm_method_1")] <- col
当尝试使用我预先准备的5个元素的列表更新数据帧的第一列(最后一行)时(数据帧有5行),我收到以下错误:
Error in `[<-.data.frame`(`*tmp*`, , c("norm_method_1"), value = list( :
replacement element 1 is a matrix/data frame of 2 rows, need 5
知道为什么吗?
更新:
dim(col)
NULL
str(col)
> str(col)
List of 5
$ : num [1:2, 1:30] 21.436 40.828 -0.785 0.54 -1.375 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:2] "2" "3"
.. ..$ : chr [1:30] "(Intercept)" "is_top_rated_listing1" "seller_is_top_rated_seller" "is_auto_pay1" ...
$ :List of 5
..$ positive: NULL
..$ table : 'table' int [1:3, 1:3] 414 73 433 101 329 1186 147 60 10546
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ Prediction: chr [1:3] "1" "2" "3"
.. .. ..$ Reference : chr [1:3] "1" "2" "3"
..$ overall : Named num [1:7] 0.849 0.402 0.843 0.856 0.809 ...
.. ..- attr(*, "names")= chr [1:7] "Accuracy" "Kappa" "AccuracyLower" "AccuracyUpper" ...
..$ byClass : num [1:3, 1:8] 0.45 0.204 0.981 0.98 0.989 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:3] "Class: 1" "Class: 2" "Class: 3"
.. .. ..$ : chr [1:8] "Sensitivity" "Specificity" "Pos Pred Value" "Neg Pred Value" ...
..$ dots : list()
..- attr(*, "class")= chr "confusionMatrix"
$ :Classes 'auc', 'numeric' atomic [1:1] 0.592
.. ..- attr(*, "partial.auc")= logi FALSE
.. ..- attr(*, "percent")= logi FALSE
.. ..- attr(*, "roc")=List of 8
.. .. ..$ percent : logi FALSE
.. .. ..$ sensitivities: num [1:1021] 1 0.998 0.998 0.998 0.996 ...
.. .. ..$ specificities: num [1:1021] 0 0 0.00151 0.00302 0.00302 ...
.. .. ..$ thresholds : num [1:1021] -Inf 0.358 0.359 0.36 0.361 ...
.. .. ..$ direction : chr "<"
.. .. ..$ cases : num [1:462] 0.51 0.727 0.725 0.667 0.667 ...
.. .. ..$ controls : num [1:662] 0.903 0.967 0.756 0.966 0.981 ...
.. .. ..$ fun.sesp :function (thresholds, controls, cases, direction)
.. .. ..- attr(*, "class")= chr "roc"
$ : num [1:28, 1:4] 3.18 3.49 -3.55 -1.16 4.1 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:28] "(Intercept)" "is_top_rated_listing1" "seller_is_top_rated_seller" "is_auto_pay1" ...
.. ..$ : chr [1:4] "Estimate" "Std. Error" "t value" "Pr(>|t|)"
$ : num 0.819
>
答案 0 :(得分:0)
如果遇到这种情况,您需要检查数据的大小是否符合您的想法。使用
length(col)
验证您对col的假设。消息是说尺寸不兼容。
如果是,那么作业将起作用 - 例如,
col <- c(1,2,3,4,5)
eval_table[,c("norm_method_1")] <- col
在这种情况下,length(col)
返回5.
另一个有用的命令是str
- 即str(eval_table)
和str(col)
。这将帮助您查看大小不兼容。
在这种情况下,您的第一个元素是矩阵。所以我们可以这样嘲笑:
mat <- matrix(1,2)
col <- list(mat, 2, 3, 4, 5)
然后你会看到你得到的错误。要解决这个问题,请使用矩阵的第一个元素作为第一个元素构建列表,并将其构建为向量:
col <- c(mat[1][1], 2,3, 4,5)
然后这个有效:
eval_table[,c("norm_method_1")] <- col