【问题标题】:Storing values from for loop in R在R中存储来自for循环的值
【发布时间】:2021-09-10 03:11:29
【问题描述】:

我希望将“roc_full_resolution”中的值保存到向量中。有什么想法吗?

 for(i in 1:10) repeat {
CarefulR<- merge(data,reliable,by.x ="Response_ID" )
CarefullRespondents <- CarefulR %>% select(Response_ID,Category,Q17_1,Q17_2,Q17_3,Q17_4,Q17_5,Q17_6,Q17_7,Q17_8,Q17_9,Q17_10,Q17_11,Q17_12,Q17_13,Q17_14,Q17_15,Q17_16,Q17_17,Q17_18,Q17_19,Q17_20,Q17_21,Q17_22,Q17_23,Q17_24)
CarelessRespondents<- unreliable %>% select(Response_ID,Category,Q17_1,Q17_2,Q17_3,Q17_4,Q17_5,Q17_6,Q17_7,Q17_8,Q17_9,Q17_10,Q17_11,Q17_12,Q17_13,Q17_14,Q17_15,Q17_16,Q17_17,Q17_18,Q17_19,Q17_20,Q17_21,Q17_22,Q17_23,Q17_24)
CR2<- sample_n(CarelessRespondents, 146,replace = TRUE)
df <- rbind(CarefullRespondents,CR2)[-1]
simulation1_mahad <- mahad_raw <- mahad(df )
rounded_scores <- round(simulation1_mahad, digits=1)
roc_rounded <- roc(df$Category, rounded_scores)
roc_full_resolution <- roc(df$Category,rounded_scores)
print(roc_full_resolution)
break}

【问题讨论】:

标签: r loops vector


【解决方案1】:

我会在 for 循环之前创建 roc_full_resolution 变量,然后 append 每次迭代的结果。我还将设置移到循环之外,我认为在这种情况下不需要repeatbreak

CarefulR <- merge(data, reliable, by.x = "Response_ID")
CarefullRespondents <- CarefulR %>% select(Response_ID,Category,Q17_1,Q17_2,Q17_3,Q17_4,Q17_5,Q17_6,Q17_7,Q17_8,Q17_9,Q17_10,Q17_11,Q17_12,Q17_13,Q17_14,Q17_15,Q17_16,Q17_17,Q17_18,Q17_19,Q17_20,Q17_21,Q17_22,Q17_23,Q17_24)
CarelessRespondents <- unreliable %>% select(Response_ID,Category,Q17_1,Q17_2,Q17_3,Q17_4,Q17_5,Q17_6,Q17_7,Q17_8,Q17_9,Q17_10,Q17_11,Q17_12,Q17_13,Q17_14,Q17_15,Q17_16,Q17_17,Q17_18,Q17_19,Q17_20,Q17_21,Q17_22,Q17_23,Q17_24)
roc_full_resolution<-NULL
for(i in 1:10) {
    CR2 <- sample_n(CarelessRespondents, 146, replace = TRUE)
    df <- rbind(CarefullRespondents, CR2)[-1]
    simulation1_mahad <- mahad_raw <- mahad(df)
    rounded_scores <- round(simulation1_mahad, digits = 1)
    roc_rounded <- roc(df$Category, rounded_scores)
    roc_full_resolution <- append(roc_full_resolution, roc_rounded)
  }
print(roc_full_resolution)

【讨论】:

  • 试过了,它产生了一个空向量
  • 如果没有可重现的示例,我将无法进行故障排除。最快的方法是检查你的输出……roc_rounded 有数据吗? rounded_scores 有数据吗? df有数据吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-21
  • 1970-01-01
  • 2016-11-25
  • 2013-12-07
  • 2015-08-25
  • 1970-01-01
  • 2021-07-28
相关资源
最近更新 更多