我已经创建了一个矩阵列表,现在我想获取所有矩阵中每行的最大值,该怎么做?
以下是列表的代码:
i <- 1
tryList <- list()
treeList <- list()
accList <- list()
for(t_mtry in 1:40){
for(t_ntree in 20:300{
rf <- randomForest(class ~., data=training, mtry=t_mtry, ntree=t_ntree)
tbl <- table(predicted = predict(rf,evalSet,type="class"),actual=evalSet$class)
#get the accuracy of the classification as a list
retVal <- accuracy(tbl)
tryList <- c(tryList,t_mtry)
treeList <- c(treeList,t_ntree)
accList <- c(accList,mean(retVal))
}
matrixList[[i]] <- matrix(c(tryList,treeList,accList),length(accList)
i <- i + 1
tryList <- list()
treeList <- list()
accList <- list()
}
现在我想要从每个矩阵中获取accList的最大值。 如果我只有一个矩阵,我可以使用以下代码:
lapply(matrix,max)
max(unlist(matrix[,3]))
但是我如何将它与列表一起使用?
matrix
上使用lapply,你会评估每个元素。你能提供一个可重现的例子吗(这个例子无法运行,有两个括号缺失,并且library(randomForest)没有声明)? - Roman Luštrik