简体   繁体   中英

R how to print different types of variables into one txt file

I have a lm model, a numeric vector from vif function, and some character variables.

gvmodel<-gvlma(lmFit)
VIF<-sqrt(vif(lmFit))
Str1<-"Original R-square=".567"
Str2<-"Cross-validated R-Square=.123"

I would like to print content of all into one single txt file. I tried cat and capture.output .

cat(gvmodel,VIF,Str1, file="E:/.../text.txt")
capture.output(paste(gvmodel,VIF,...,sep=""),file="E:/.../text.txt")

Obviously this did not work. Anyone how to print them into a single txt file? Thanks

Sink() works. Sink a txt file path and name, run any command that produces text outputs and they will be captured in the txt file. And finally sink() to close.

 sink("E:/.../Sink.txt")
 gvmodel
 sqrt(vif(ModeName))
 sink()

In your code gvmodel<-gvlma(lmFit) produces gvlma object. It cannot be directly printed to .txt file as such. You can transform it first to characters using as.character() option:

gvmodel<-gvlma(lmFit)
gvmodel <- as.character(gvmodel)

The result is printable for example with cat . So if you put all this together:

gvmodel<-gvlma(lmFit)
gvmodel <- as.character(gvmodel)
VIF<-sqrt(vif(lmFit))
Str1<-"Original R-square=.567"
Str2<-"Cross-validated R-Square=.123" # NB the corrected quotation marks

cat(gvmodel,VIF,Str1, file="yourfile.txt",sep="\t")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM