如何使输出的txt文件看起来更好 [英] How to make an output txt file look better
问题描述
我正在使用以下代码来生成一个包含glm模型的所有结果的文件.
我想保存每个模型的结果以及每个模型使用的公式.
我得到的输出有点混乱,因为每个模型在生成的txt文件中都打印为两行而不是四行.
I'm using the following code to generate a file that contains all the results of glm models.
I would like to save the results of each model and the formula that was used per each model.
The output that I get is a bit of a mess because each model is printed as two line instead of 4 rows in the generated txt file.
如何解决此问题?
玩具数据:
How can I solve this issue?
Toy data:
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,甚至当您只是将打印的输出发送到文件时,甚至还有sink
,肯定有更好的选择,例如write.table
.也就是说,对您的代码的快速修复是:
Personally, I wouldn't use capture.output
unless absolutely necessary. There are definitely better choices like write.table
if you are using a data.frame or even sink
when you are just sending printed output to a file. That said, a fast fix for your code would be:
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
,因为它没有包含在示例数据帧中.这将在文本文件中输出:
Note I am not using country
above as it is not included in your example data.frame. This outputs in the text file:
[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
相同,就像(对我来说似乎更容易):
The same using sink
would be like (seems easier to me):
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()
}
这篇关于如何使输出的txt文件看起来更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!