【问题标题】:Add new row to R data frame in for loop without using NROW()在 for 循环中向 R 数据框添加新行而不使用 NROW()
【发布时间】:2020-04-10 00:07:06
【问题描述】:

问题

在函数中的数据框中添加行以进行精度测量。

R 中的努力

函数 compute_accuracy.func() 返回精度准确度测量值。 IF 循环调用 compute_accuracy.func() 并向 data.frame 添加新行。

向数据框添加新行,不起作用

需要为每个 for...循环添加新行,使用阈值并计算精度。我是在 for...loop 中动态地将新行添加到 R data.frame 的新手。

compute_accuracy.func <- function(t_threshold) {
  tryCatch({
    x <- accuracy.meas(full$y, loans_predict$fit, threshold=t_threshold)
    return(x$precision) 
  }, 
  error = function(e) return(e)
  )
}

df_accuracies <- data.frame(n=0, threshold=0, precision=0)
compute_for_values = seq(0.1,0.9,by=0.1)
for(i in compute_for_values){
  threshold = i
  precision <- compute_accuracy.func(i)
  df_accuracies[nrow(df_accuracies) + 1,] <- rbind(threshold, precision)
}

【问题讨论】:

    标签: r dataframe


    【解决方案1】:

    通过这种方法解决:采用向量方法而不是 rbind,例如,c() 与 c() 中的变量。

    df_accuracies <- data.frame(n=0, threshold=0, precision=0)
    compute_for_values = seq(0.15,0.95,by=0.1)
    n=0
    for(i in compute_for_values){
      n <- n+1
      threshold = i
      precision <- compute_accuracy.func(i)
      df_accuracies[nrow(df_accuracies) + 1,] <- c(n, threshold, precision)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多