[R] Executing Command on Multiple R Objects

David Winsemius dwinsemius at comcast.net
Mon Nov 15 23:11:27 CET 2010


On Nov 15, 2010, at 4:43 PM, patze003 wrote:

>
> Hello Everyone -
>
> I want to print a number of results from lme function objects out to  
> a txt
> file.  How could I do this more efficiently than what you see here:
>
> out2 <- capture.output(summary(mod2a))
> out3 <- capture.output(summary(mod3))
> out4 <- capture.output(summary(mod5))
> out5 <- capture.output(summary(mod6))
> out6 <- capture.output(summary(mod7))
> cat(out2,file="out.txt",sep="\n",append=TRUE)
> cat(out3,file="out.txt",sep="\n",append=TRUE)
> cat(out4,file="out.txt",sep="\n",append=TRUE)
> cat(out5,file="out.txt",sep="\n",append=TRUE)
> cat(out6,file="out.txt",sep="\n",append=TRUE)
> cat(third,file="out.txt",sep="\n",append=TRUE)
>
>
> Here's an example of what I tried, but didn't work.
>
> for (i in ls(pat = "mod"))
> 	{ out <- capture.output(summary[[i]])
> 		cat(out, file = "results_paired.txt", sep = "\n", append = TRUE)
> 		}

You could have done something along these lines:

for (i in ls(pat = "mod"))
	{ out <- capture.output( summary(get(i)),
                            file = paste("results_paired",".txt", sep  
= "."),
                            append = TRUE)
		}

Note use of get() and not sending to an R object but rather using  
capture.output()'s side effect strategy.

> -- 


David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list