[R] how to create matrix in a loop

John Kane jrkrideau at yahoo.ca
Fri Jul 13 15:47:38 CEST 2007

You don't need to do a loop.  You can do it with an
apply statement I think. See ?apply

If the matrix is "mat" to do the test by column try:

apply(mat, 2, shapiro.test)

--- Tavpritesh Sethi <tavpritesh at gmail.com> wrote:

> Hi all,
> How does one create a matrix of values in a loop.
> For example, I have 46*70
> matrix with columns giving the values for some blood
> tests performed upon
> the 46 patients(rows). I want to perform a simple
> test of normality
> (shapiro-wilk test) upon each of the biochemical
> tests for the 46
> patients. Can somebody suggest the syntax for such a
> loop. For the data x, I
> wrote the following loop which didnt work:-
> pv<-1:70
> stat<-1:70
> for(i in 1:70)
> {
> pv[i]=shapiro.test(x[,i])$p.value
> stat[i]<-shapiro.test(x[,i])$statistic
> }
> warning msg: items to replace is not a multiple of
> replacement length
> Does it have anything to do with the method of
> importing data as
> read.delimrather than
> read.table?
> Please suggest

More information about the R-help mailing list