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 LSU _________________________________________________________________ [[elided Hotmail spam]] [[alternative HTML version deleted]]
Perhaps like this: diag(mat[data$row, data$column]) <- data$height On 29/01/2008, Gator Connection <gatorconnection at hotmail.com> 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 > LSU > _________________________________________________________________ > [[elided Hotmail spam]] > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
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. > JackHi 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 newmat<-matrix(0,nrow=5,ncol=3) newmat[x[,1],x[,2]]<-x[,3] would do it, but it doesn't. However, # scramble the matrix x1<-x[sample(1:15,15),] # 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