[R] matrix with Loop

Christopher Kelvin chris_kelvin2001 at yahoo.com
Thu Mar 29 15:00:21 CEST 2012


Hello!
I got something to ask..whether you can help me with the R program...i got this for example 5x4 matrix..and i want to find:
 i) mean for each row of the matrix
ii) median for each column of the matrix
and i need to do this using a loop function...below is my program..u try to check it for me as the output that i got is not what i desired...thanks..

data<-rnorm(20,0,1)
dim(data)<-c(5,4)
is.matrix(data)
data
a<-function(x)
{
for(i in 1:nrow(x))
{
for(j in 1:ncol(x))
{
med<-median(x[,j]) 
mean<-mean(x[i,])
print(c(med=med,mean=mean))
}
}
}
a(data) 

Chris Guure
Researcher
Institute for Mathematical Research
UPM



More information about the R-help mailing list