apply(x, 1, mean) apply(x, 2, median) or for more speed rowMeans()
? apply Michael On Thu, Mar 29, 2012 at 9:00 AM, Christopher Kelvin <chris_kelvin2...@yahoo.com> wrote: > 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 > > ______________________________________________ > R-help@r-project.org 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-help@r-project.org 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.