在For-Loop内部的信宿给出错误“信宿栈已满”在R [英] Sink inside a For-Loop gives the error "sink stack is full" in R

查看:184
本文介绍了在For-Loop内部的信宿给出错误“信宿栈已满”在R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在R中使用以下数据:

 > str(df)
'data.frame':369269 obs。 12个变量:
$ bkod:int 110006 110006 110006 110006 110006 110006 110006 110006 110006 110006 ...
$ bad:因子w / 215等级4. Levent,500 Evler,.. :26 26 26 26 26 26 26 26 26 26 ...
$ mkod:int 359 359 359 359 359 359 359 359 359 359 ...
$ mad:因数为8643的水平Hilal Gida 4021 4021 4021 4021 4021 4021 4021 4021 4021 4021 ...
$ yekod:int 12 12 12 12 12 12 12 12 12 ...
$ yad:因数为44等级BAKUGAN,BARBIE,...:1 1 1 1 1 2 2 2 2 2 ...
$已完成:诠释201101 201103 201105 201107 201109 201102 201103 201104 201106 201107 ...
$ sayi:int 201101 201102 201103 201104 201105 201102 201103 201104 201106 201107 ...
$计划:num 5 3 2 7 5 0 2 2 0 2 ...
$ sevk:int 5 6 4 7 6 3 2 5 5 2 ...
$ iade:int 4 1 3 5 5 2 2 5 0 2 ...
$ satis:int 1 5 1 2 1 1 0 0 5 0 .. 。

bkv< - unique(df $ bkod)
> bkv
[1] 110006 110007 110010 110011 110020 110022 110027 110030 110032 110036 110043 110049 110051 110054 110056 110061 110062 110067 110069 110071 110077
[22] 110084 110092 110093 110094 110100 110101 110104 110109 110111 110113 110116 110117 110120 110133 110138 110143 110158 110160 110162 110186 110187
[43] 110198 110202 110208 110213 110214 110216 110218 110219 110222 110224 110229 110232 110234 110235 110247 110249 110250 110252 110255 110256 110262
[64] 110265 110266 110268 110269 110270 110274 110277 110278 110282 110285 110295 110296 110301 110306 110309 110310 110311 110313 110314 110315 110319
[85] 110329 110346 110351 110354 110357 110359 110377 110379 110380 110386 110392 110394 110396 110397 110400 110406 110412 110413 110419 110421 110422
[106] 110423 110426 110428 110431 110439 110442 110444 110452 110455 110464 110467 110469 110470 110471 110475 110478 110480 110485 110490 110492 110498
[127] 110499 110503 110505 110506 110508 11 0509 110512 110514 110517 110518 110519 110523 110532 110534 110537 110540 110543 110544 110545 110546 110547
[148] 110549 110550 110551 110553 110554 110555 110556 110557 110559 110560 110562 110565 110566 110569 110570 110571 110572 110573 110575 110576 110577
[169 ] 110578 110579 110580 110581 110582 110583 110584 110586 110587 110588 110589 110590 110591 110594 110595 110596 110597 110598 110599 110600 110602
[190] 110603 110605 110606 110607 110608 110609 110610 110611 110612 110613 110614 110615 110616 110617 110619 110620 110621 110622 110623 110624 110625
[211] 110626 110627 110628 110629 110630

我想要做的是让一些对 bkv 中的每个元素进行计算,然后在For循环中使用 sink 将结果追加到txt文件中。所以我创建了下面的循环:

$ p $ for(i in bkv){
new < - df [df $ bkod == i,]
dp < - new [new $ plan!= new $ sevk,]
medy < - 100 *(nrow(dp)/ nrow(new))
sink(Report.txt)
cat(i,med,\\\

}

但是会出现以下错误:
$ b


Report.txt):sink stack已满

它创建Report.txt,但是它是一个空文件。该报告应该有215行作为 bkv 有215个元素。



我做错了什么?我应该怎么做才能使这个循环正常工作,并将结果追加到文本文件中?

解决方案功能 sink 设置输出到文件的转移。你只在循环之前做一次。然后 cat 的输出将被转移到你的sink文件中,直到你用 sink()取消接收器。 / p>

所以你的代码看起来应该是这样的:

  .txt)
for(i in bkv){
new < - df [df $ bkod == i,]
dp < - new [new $ plan!= new $
medy < - 100 *(nrow(dp)/ nrow(new))
cat(i,içinmüdahaleoranı:%,medy,\\\


$(b










$可能有一堆 sink 文件。这个堆栈的最大大小将取决于您的操作系统将允许多少个打开的文件句柄。





警告



我使用 sink 作为R newby。但是我觉得它有一些危险的副作用,你应该注意一下。最重要的是,如果你的脚本有错误,并且你重新运行脚本,那么你的 sink(filename)会被添加到接收器堆栈中。因此,除非你对 sink 堆栈的记录一丝不苟,否则会导致各种错误。

在我看来,使用 cat 的能力追加到文件:

  cat(...,file =Report.txt,append = TRUE)

或者,甚至更好,编写一个包装函数。

  catf<  -  function(...,file =report.txt,append = TRUE){
cat(...,file = file,append = append)
}


I am working with the following data in R:

> str(df)
'data.frame':   369269 obs. of  12 variables:
 $ bkod : int  110006 110006 110006 110006 110006 110006 110006 110006 110006 110006 ...
 $ bad  : Factor w/ 215 levels "4. Levent","500 Evler",..: 26 26 26 26 26 26 26 26 26 26 ...
 $ mkod : int  359 359 359 359 359 359 359 359 359 359 ...
 $ mad  : Factor w/ 8643 levels "    Hilal Gida           ",..: 4021 4021 4021 4021 4021 4021 4021 4021 4021 4021 ...
 $ yekod: int  12 12 12 12 12 12 12 12 12 12 ...
 $ yad  : Factor w/ 44 levels "BAKUGAN","BARBIE",..: 1 1 1 1 1 2 2 2 2 2 ...
 $ donem: int  201101 201103 201105 201107 201109 201102 201103 201104 201106 201107 ...
 $ sayi : int  201101 201102 201103 201104 201105 201102 201103 201104 201106 201107 ...
 $ plan : num  5 3 2 7 5 0 2 2 0 2 ...
 $ sevk : int  5 6 4 7 6 3 2 5 5 2 ...
 $ iade : int  4 1 3 5 5 2 2 5 0 2 ...
 $ satis: int  1 5 1 2 1 1 0 0 5 0 ...

bkv <- unique(df$bkod)
> bkv
  [1] 110006 110007 110010 110011 110020 110022 110027 110030 110032 110036 110043 110049 110051 110054 110056 110061 110062 110067 110069 110071 110077
 [22] 110084 110092 110093 110094 110100 110101 110104 110109 110111 110113 110116 110117 110120 110133 110138 110143 110158 110160 110162 110186 110187
 [43] 110198 110202 110208 110213 110214 110216 110218 110219 110222 110224 110229 110232 110234 110235 110247 110249 110250 110252 110255 110256 110262
 [64] 110265 110266 110268 110269 110270 110274 110277 110278 110282 110285 110295 110296 110301 110306 110309 110310 110311 110313 110314 110315 110319
 [85] 110329 110346 110351 110354 110357 110359 110377 110379 110380 110386 110392 110394 110396 110397 110400 110406 110412 110413 110419 110421 110422
[106] 110423 110426 110428 110431 110439 110442 110444 110452 110455 110464 110467 110469 110470 110471 110475 110478 110480 110485 110490 110492 110498
[127] 110499 110503 110505 110506 110508 110509 110512 110514 110517 110518 110519 110523 110532 110534 110537 110540 110543 110544 110545 110546 110547
[148] 110549 110550 110551 110553 110554 110555 110556 110557 110559 110560 110562 110565 110566 110569 110570 110571 110572 110573 110575 110576 110577
[169] 110578 110579 110580 110581 110582 110583 110584 110586 110587 110588 110589 110590 110591 110594 110595 110596 110597 110598 110599 110600 110602
[190] 110603 110605 110606 110607 110608 110609 110610 110611 110612 110613 110614 110615 110616 110617 110619 110620 110621 110622 110623 110624 110625
[211] 110626 110627 110628 110629 110630

What I want to achieve is to make some calculations with respect to each element in bkv and append the results to a txt file using sink inside a For-Loop. So I created the following loop;

for (i in bkv) {
  new <- df[df$bkod==i,]
  dp <- new[new$plan!=new$sevk,]
  medy <- 100*(nrow(dp)/nrow(new))
  sink("Report.txt")
  cat(i, "için müdahale oranı: %", medy, "\n")
}

but it gives the following error:

Error in sink("Report.txt") : sink stack is full

It creates Report.txt but it's an empty file. The report should have 215 lines as bkv has 215 elements.

What am I doing wrong? What should I do to make this loop work correctly and append the results to the text file?

解决方案

The function sink sets up a diversion of output to a file. You only do this once, before the loop. The output of cat will then get diverted into your sink file, until you cancel the sink with sink().

So your code should look something like:

sink("Report.txt")
for (i in bkv) {
  new <- df[df$bkod==i,]
  dp <- new[new$plan!=new$sevk,]
  medy <- 100*(nrow(dp)/nrow(new))
  cat(i, "için müdahale oranı: %", medy, "\n")
}
sink()


It is possible to have a stack of sink files. The maximum size of this stack will depend on how many open file handles your operating system will allow.


Warning

I used sink quite a bit as an R newby. But I think it has some dangerous side effects that you should take note of. The most important is that if your script has errors, and you rerun the script, then your sink(filename) will get added to the stack of sinks.

So unless you are scrupulous about your book-keeping of the sink stack, this can lead to all kinds of errors.

In my view it is far better to use the ability of cat to append to a file, e.g:

cat(..., file="Report.txt", append=TRUE)

Or, even better, write a wrapper function. Something along the lines of:

catf <- function(..., file="report.txt", append=TRUE){
  cat(..., file=file, append=append)
}

这篇关于在For-Loop内部的信宿给出错误“信宿栈已满”在R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆