r - 如何使输出的txt文件看起来更好

标签 r loops

我使用以下代码生成一个包含 glm 模型的所有结果的文件。
我想保存每个模型的结果以及每个模型使用的公式。
我得到的输出有点困惑,因为每个模型在生成的 txt 文件中打印为两行而不是 4 行。

如何解决这个问题?
玩具数据:

df <- read.table(text = "target birds    wolfs     
                        0       21         7  
                        0        8         4  
                        1        2         5 
                        1        2         4 
                        0        8         3 
                        1        1         12  
                        1       7          10 
                        1        1         9 ",header = TRUE)

代码:

myform <-NULL
 myform <- target~1
 dd<-NULL
 for ( i in c('birds', 'wolfs' )) { 
     myform <- update(myform,  as.formula(paste('~ birds +', i)))
     glm<-glm(myform,data=df)
     df$glm_predict_response <- ifelse(predict(glm,newdata=df ,   type="response")>.5, 1, 0)
    ff<-print(myform)
    dd<-print(xtabs(~ target + glm_predict_response, data = df ))
     print(prop.table(xtabs(~target + glm_predict_response, data = df ), 2) )
     e<-capture.output(ff,append = TRUE)
    e1<-capture.output(dd,append = TRUE)
    capture.output(e,e1, file = "myform2.txt",append = TRUE)
 }

txt 文件的输出:

[1] "target ~ birds"
[1] "      glm_predict_response" "target 0 1"                 "     0 1 2"                 "     1 0 5"                
[1] "target ~ birds + wolfs"
[1] "      glm_predict_response" "target 0 1"                 "     0 3 0"                 "     1 0 5"                
[1] "target ~ birds + Country"
[1] "      glm_predict_response" "target 0 1"                 "     0 3 0"                 "     1 0 5"

最佳答案

就我个人而言,除非绝对必要,否则我不会使用capture.output。如果您使用 data.frame,那么肯定有更好的选择,例如 write.table,或者当您只是将打印输出发送到文件时甚至 sink。也就是说,代码的快速修复方法是:

myform <-NULL
myform <- target~1
dd<-NULL
for ( i in c('birds', 'wolfs')) { 

  myform <- update(myform,  as.formula(paste('~ birds +', i)))
  glm<-glm(myform,data=dat)
  dat$glm_predict_response <- ifelse(predict(glm,newdata=dat,   type="response")>.5, 1, 0)
  ff<-print(myform)
  dd<-print(xtabs(~ target + glm_predict_response, data = dat))
  print(prop.table(xtabs(~target + glm_predict_response, data = dat), 2) )
  e<-capture.output(ff,append = TRUE)
  #you really don't need the second capture.output since you can
  #just print dd to the file directly
  capture.output(e, dd, file = "myform2.txt",append = TRUE)
}

请注意,我没有使用上面的country,因为它未包含在您的示例 data.frame 中。这在文本文件中输出:

[1] "target ~ birds"
      glm_predict_response
target 0 1
     0 1 2
     1 0 5
[1] "target ~ birds + wolfs"
      glm_predict_response
target 0 1
     0 3 0
     1 0 5

同样使用sink会像(对我来说似乎更容易):

myform <-NULL
myform <- target~1
dd<-NULL
for ( i in c('birds', 'wolfs')) { 

  myform <- update(myform,  as.formula(paste('~ birds +', i)))
  glm<-glm(myform,data=dat)
  dat$glm_predict_response <- ifelse(predict(glm,newdata=dat,   type="response")>.5, 1, 0)
  dd<-xtabs(~ target + glm_predict_response, data = dat)

  #start sinking
  sink(file='myform2.txt', append=TRUE)
  print(myform)
  cat('\n\n')
  print(dd)
  cat('\n\n')
  #stop sinking
  sink()
}

关于r - 如何使输出的txt文件看起来更好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33232745/

相关文章:

c# - 使用 R(D)COM 将 R 与 C# 集成

r - 在 R 中求矩阵内 block 的总和

r - data.table高效回收

mysql - 嵌套游标循环: Column count doesn't match value count at row 1

r - 如何在 R 中执行自然(词典)排序?

R markdown word文档输出边距

java - 如果生命游戏中的语句重置板而不是遵守规则

javascript - 为什么在映射数组时使用它传递给函数时索引始终是最后一个索引?

javascript - 如何仅列出数组中包含 '3' 的数字?

php - WHILE 循环不能使用两次? PHP