Re: [R] help on establishing a matrix

From: Jim Lemon <>
Date: Thu, 31 Jan 2008 19:57:44 +1100

Gator Connection wrote:
> I am a beginner and this is a naive question.
> I have the following data set.
> row column height
> 1 2 96
> 3 7 67
> 9 25 77
> ......
> I have a matrix of 50*100 data points and about 60% of them are zeros.
> I want to put the height data into the matrix according to their row and column numbers. does anybody have experience in setting up such matrix?
> Your help is highly appreciated.
> Jack
Hi Jack,
Well, what do you know? I thought that this:

x<-matrix(c(rep(1:5,each=3),rep(1:3,5),sample(10:100,15)),ncol=3) x

would do it, but it doesn't. However,

# scramble the matrix
# create a new matrix by ordering it by row and column newmat<-matrix(x1[order(x1[,1],x1[,2]),3],ncol=3,byrow=TRUE) newmat

works for me

Jim mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Thu 31 Jan 2008 - 08:56:33 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 31 Jan 2008 - 09:30:10 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive