Re: [R] RE : for loops

2007-04-25 Thread Petr Klasterecky
I know that a looping solution was requested, but this is exactly what 
apply() should be used for...

Petr

justin bem napsal(a):
> You can see with this simple example. 
> 
> matrix.t.test<-function(mx){
> 
> p<-dim(mx)[2]  #number of column in the matrix
> n<-dim(mx)[1]  #number of row
> 
> n.tests<- p*(p-1)/2 #Number of tests to be done
> 
> tested.var <-rep("",n.tests) #Keep rang of tested
> column
> r.t.stat<-rep(0, n.tests)#contain t.stat
> r.p.val <-rep(0, n.tests)#contain p.values
> 
> ctst<-1 #current test
> for (i in 1:(p-1)){
>   for (j in (i+1):p){
> r.t.stat[ctst]<-t.test(mx[,i],mx[,j])$statistic
> r.p.val [ctst] <-t.test(mx[,i],mx[,j])$p.value
> tested.var [ctst]<-paste(i,"-",j)
> ctst<-ctst+1
>   }
> }
> 
> result<-data.frame(tested.var,r.t.stat,r.p.val)
> return(result)
> }
> 
> matrix.t.test(matrix(rnorm(50),nr=10,nc=5))
> 
> 
> 
> 
> 
> 
> 
> 
> 
> --- "[EMAIL PROTECTED]"
> <[EMAIL PROTECTED]> a écrit :
> 
>> Hello everybody
>> I'm very new at using R so probably this is a very
>> stupid question.
>> I have a matrix of "p" columns and I have to
>> calculate for each of them the "two sample
>> t-statistic" and p-value and to save the results
>> into two different vectors.
>> I have divided my matrix into two submatrices:
>> submatrix A containing the first "n1" rows (p
>> columns) and submatrix B containing the remaining
>> "n2" (total rows=n1+n2).
>> How can I do this with for loop construction?
>> Friendly regards
>> Silvia
>>
>>
>>
> --
>> Passa a Infostrada. ADSL e Telefono senza limiti e
>> senza canone Telecom
>> http://click.libero.it/infostrada
>>
>> __
>> R-help@stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained,
>> reproducible code.
>>
> 
> 
> Justin BEM
> Elève Ingénieur Statisticien Economiste
> BP 294 Yaoundé.
> Tél (00237)9597295.
> 
> __
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 

-- 
Petr Klasterecky
Dept. of Probability and Statistics
Charles University in Prague
Czech Republic

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] RE : for loops

2007-04-25 Thread justin bem
You can see with this simple example. 

matrix.t.test<-function(mx){

p<-dim(mx)[2]  #number of column in the matrix
n<-dim(mx)[1]  #number of row

n.tests<- p*(p-1)/2 #Number of tests to be done

tested.var <-rep("",n.tests) #Keep rang of tested
column
r.t.stat<-rep(0, n.tests)#contain t.stat
r.p.val <-rep(0, n.tests)#contain p.values

ctst<-1 #current test
for (i in 1:(p-1)){
  for (j in (i+1):p){
r.t.stat[ctst]<-t.test(mx[,i],mx[,j])$statistic
r.p.val [ctst] <-t.test(mx[,i],mx[,j])$p.value
tested.var [ctst]<-paste(i,"-",j)
ctst<-ctst+1
  }
}

result<-data.frame(tested.var,r.t.stat,r.p.val)
return(result)
}

matrix.t.test(matrix(rnorm(50),nr=10,nc=5))









--- "[EMAIL PROTECTED]"
<[EMAIL PROTECTED]> a écrit :

> Hello everybody
> I'm very new at using R so probably this is a very
> stupid question.
> I have a matrix of "p" columns and I have to
> calculate for each of them the "two sample
> t-statistic" and p-value and to save the results
> into two different vectors.
> I have divided my matrix into two submatrices:
> submatrix A containing the first "n1" rows (p
> columns) and submatrix B containing the remaining
> "n2" (total rows=n1+n2).
> How can I do this with for loop construction?
> Friendly regards
> Silvia
> 
> 
>
--
> Passa a Infostrada. ADSL e Telefono senza limiti e
> senza canone Telecom
> http://click.libero.it/infostrada
> 
> __
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained,
> reproducible code.
> 


Justin BEM
Elève Ingénieur Statisticien Economiste
BP 294 Yaoundé.
Tél (00237)9597295.

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.