加快 R 中嵌套 for 循环的速度,并在 excel 中存储超出其限制的非常大的数据框
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)