[R] Naming output of a for loop

Steve Lianoglou mailinglist.honeypot at gmail.com
Mon Mar 1 18:37:00 CET 2010


Hi Kevin,

On Mon, Mar 1, 2010 at 12:30 PM, Kevin Long
<kevin.diedrich.long at gmail.com> wrote:
> Hello R users,
>
> I am new to R and am having difficulty with the output of my "for" loops. I
> have written a function to perform a linear regression on multiple files,
> but when I use the "print()" function to look at my results, it is not
> specified from which data set the regression was done on. I would like the
> output from my for loops to specify which data set the regression
> corresponds to. The following code following this text is an example of what
> I used to perform the regression. I would like my output from the loop to
> label each regression based on the data used for the analysis.
>
> Thank-you for your help
>
> Kevin Long
>
> Example Code:
>
> setwd("/Users/kevinlong/Desktop/R Example")
> list <- list.files()
> for (x in list) {u <- read.table(x,header = T, sep=",")
>    regression <- lm(u$R~u$T)
>    print(regression)
>    }

What bout just printing the name of the file before printing its summary?
Eg:

for (x in list) {
  u <- read.table(x,header = T, sep=",")
  regression <- lm(u$R~u$T)
  cat("File:", x, "\n")
  print(regression)
}

You might want to put your regressions in a list object if you want to
access them later ... but I'll leave that for another question.

-steve

-- 
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact



More information about the R-help mailing list