加快 R 中嵌套 for 循环的速度,并在 excel 中存储超出其限制的非常大的数据框

发布于 2025-01-13 02:58:17 字数 841 浏览 0 评论 0原文

我在 R 中使用以下代码行花了 36 个小时或更长时间来努力创建一个数据框。任何使此代码更快的建议都将受到赞赏。然而,到最后,我需要将此数据框存储在 Excel 文件中,但我注意到此特定代码需要超过 3 亿行,这超出了典型的 Excel 工作表长度。我期待获得帮助,将其存储在 Excel(或记事本)文件中以供将来使用。

library(writexl)
team_b <- 10:120
individual_b <- 1:84
team_s <- 1:250
individual_s <- 1:150

d <- data.frame()
for (i in team_b) {
  for (j in individual_b) {
    for (k in team_s) {
      for (l in individual_s) {
        sc <- l/k 
        bu <- j/i 
        sr <- l/j 
        pi <- sc/bu
        if(bu>0.7||sc>0.7||sr>6){
          c = "unrealistic"
        }
        else{
          c = "realistic"
        }
        d <- rbind(d, data.frame(i,j,k,l,sc,bu,sr,pi,c)) 
      }
    }
  }
}
colnames(d) <- c("T_b", "I_b", "T_s", "I_s", "BU", "SC", "SR", "PI", "Comment")
#View(d)

write_xlsx(d, "d.xlsx")

I'm struggling to create a dataframe using the following codelines in R for complete 36 hours and more. Any suggestions to make this code faster would be admired. However, by the end, I need to store this data frame in an excel file, but I noticed that this particular code required over 300 million rows, which exceeds the typical excel sheet length. I look forward to get help in storing this in an excel (or notepad) file for future use as well.

library(writexl)
team_b <- 10:120
individual_b <- 1:84
team_s <- 1:250
individual_s <- 1:150

d <- data.frame()
for (i in team_b) {
  for (j in individual_b) {
    for (k in team_s) {
      for (l in individual_s) {
        sc <- l/k 
        bu <- j/i 
        sr <- l/j 
        pi <- sc/bu
        if(bu>0.7||sc>0.7||sr>6){
          c = "unrealistic"
        }
        else{
          c = "realistic"
        }
        d <- rbind(d, data.frame(i,j,k,l,sc,bu,sr,pi,c)) 
      }
    }
  }
}
colnames(d) <- c("T_b", "I_b", "T_s", "I_s", "BU", "SC", "SR", "PI", "Comment")
#View(d)

write_xlsx(d, "d.xlsx")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖公主 2025-01-20 02:58:18
library(data.table)
d <- CJ(team_b, individual_b, team_s, individual_s) # generate all combinations
setnames(d, c('i', 'j', 'k', 'l'))
d[, sc := l/k] 
d[, bu := j/i] 
d[, sr := l/j] 
d[, pi := sc/bu]
d[, c := ifelse(bu > 0.7 | sc > 0.7 | sr > 6, "unrealistic", "realistic")]
library(data.table)
d <- CJ(team_b, individual_b, team_s, individual_s) # generate all combinations
setnames(d, c('i', 'j', 'k', 'l'))
d[, sc := l/k] 
d[, bu := j/i] 
d[, sr := l/j] 
d[, pi := sc/bu]
d[, c := ifelse(bu > 0.7 | sc > 0.7 | sr > 6, "unrealistic", "realistic")]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文