Removing feedback signals in the R output

I have certain variables that lm in R automatically wrap backquotes / backquotes, for example. variables with colons in names.

After some processing, I try to write the variables and coefficients of a linear model using write.table . Unfortunately, exits are also written out.

How can I prevent these backlinks from being written?

To give a simple but unrealistic example:

 d <- data.frame(`1`=runif(10), y=runif(10), check.names=F) l <- lm(y ~ `1`, d) write.table(data.frame(l$coefficients), file="lm.coeffs", quote=F, sep="\t", col.names=F) 

The lm.coeffs file will obviously have `1` in the first output column, not 1 . Outside of post-processing in some other script, how to remove backlinks from output?

+6
source share
1 answer

You can do this post processing in R. Instead of the file, save the output in a variable using capture.output . Remove anchor points with gsub . Finally, print the output to a file using cat :

 report <- capture.output(write.table(data.frame(l$coefficients), quote = FALSE, sep = "\t", col.names = FALSE)) cat(gsub("`", "", report), sep = "\n", file = "lm.coeffs") 
+9
source

Source: https://habr.com/ru/post/944492/


All Articles