R:在循环中迭代随机数

R:在循环中迭代随机数,r,loops,random,iteration,R,Loops,Random,Iteration,我正在使用R编程语言。在前面的问题()中,我学习了如何迭代变量“I”的固定值循环: 有人能告诉我我做错了什么吗?是否有一种更简单的方法可以将循环中的所有结果存储到矩阵(或表)中,而无需显式定义所需的行数?计算机能为“i”的每一个新值自动添加一行吗 感谢使用随机数,您可以将代码更新为: a = rnorm(1000, 10, 10) b = rnorm(1000, 10, 5) c = rnorm(1000, 5, 10) group <- sample( LETTERS[1:2], 100

我正在使用R编程语言。在前面的问题()中,我学习了如何迭代变量“I”的固定值循环:

有人能告诉我我做错了什么吗?是否有一种更简单的方法可以将循环中的所有结果存储到矩阵(或表)中,而无需显式定义所需的行数?计算机能为“i”的每一个新值自动添加一行吗


感谢使用随机数,您可以将代码更新为:

a = rnorm(1000, 10, 10)
b = rnorm(1000, 10, 5)
c = rnorm(1000, 5, 10)
group <- sample( LETTERS[1:2], 1000, replace=TRUE, prob=c(0.5,0.5) )
group_1 <- 1:1000
#put data into a frame
d = data.frame(a,b,c, group, group_1)
d$group = as.factor(d$group)

#start the loop
#place results in table
final_table = matrix(1, nrow = 10, ncol=2)

e <- d
#here is the "i" variable
vec <- sample(100:400, 10)

for (i in seq_along(vec)) {
  d <- e
  d$group_1 = as.integer(d$group_1 > vec[i])
  d$group_1 = as.factor(d$group_1)
  
  trainIndex <- createDataPartition(d$group_1, p = .8,list = FALSE,times = 1)
  training = d[ trainIndex,]
  test  <- d[-trainIndex,]
  
  
  fitControl <- trainControl(## 10-fold CV
    method = "repeatedcv",
    number = 10,
    ## repeated ten times
    repeats = 10)
  
  TreeFit <- train(group_1 ~ ., data = training,
                   method = "rpart2",
                   trControl = fitControl)
  
  pred = predict(TreeFit, test, type = "prob")
  labels = as.factor(ifelse(pred[,2]>0.5, "1", "0"))
  con = confusionMatrix(labels, test$group_1)
  
  #update results into table
  final_table[i,1] = con$overall[1]
  final_table[i,2] = vec[i]
  
}
a=rnorm(1000,10,10)
b=rnorm(1000,10,5)
c=rnorm(1000,5,10)

谢谢大家的回答!您知道是否可以将caret::ConversionMatrix()函数与两个以上的类一起使用吗?我想出了如何手动操作,但是有没有内置的功能?非常感谢。
Error in na.fail.default(list(group_1 = c(NA_integer_, NA_integer_, NA_integer_,  : 
  missing values in object
a = rnorm(1000, 10, 10)
b = rnorm(1000, 10, 5)
c = rnorm(1000, 5, 10)
group <- sample( LETTERS[1:2], 1000, replace=TRUE, prob=c(0.5,0.5) )
group_1 <- 1:1000
#put data into a frame
d = data.frame(a,b,c, group, group_1)
d$group = as.factor(d$group)

#start the loop
#place results in table
final_table = matrix(1, nrow = 10, ncol=2)

e <- d
#here is the "i" variable
vec <- sample(100:400, 10)

for (i in seq_along(vec)) {
  d <- e
  d$group_1 = as.integer(d$group_1 > vec[i])
  d$group_1 = as.factor(d$group_1)
  
  trainIndex <- createDataPartition(d$group_1, p = .8,list = FALSE,times = 1)
  training = d[ trainIndex,]
  test  <- d[-trainIndex,]
  
  
  fitControl <- trainControl(## 10-fold CV
    method = "repeatedcv",
    number = 10,
    ## repeated ten times
    repeats = 10)
  
  TreeFit <- train(group_1 ~ ., data = training,
                   method = "rpart2",
                   trControl = fitControl)
  
  pred = predict(TreeFit, test, type = "prob")
  labels = as.factor(ifelse(pred[,2]>0.5, "1", "0"))
  con = confusionMatrix(labels, test$group_1)
  
  #update results into table
  final_table[i,1] = con$overall[1]
  final_table[i,2] = vec[i]
  
}