[R] Question of programming style

Chong Gu chong at stat.purdue.edu
Sat Jul 8 06:16:38 CEST 2000

   I want to do a 3d scatterplot of the upper triangle of a matrix, where the
   z-values are the values in the matrix, and the row and column indices are the y-
   and x-values. The complete (11 by 11) matrix is mmtop94.2. Here is my awkward

   mmtop94.2[lower.tri(mmtop94.2)] <- NA

   # Here i is the row (y variable), j is the column (x var)
   plotdata <- matrix(0, ncol=3, nrow=121)
   for (i in 1:11)
       for (j  in 1:11)
	   k <- (i-1) * 11 + j
	   plotdata[k,] <- c(j, abs(i - 12), mmtop94.2[i, j])
   plotdata2 <- na.omit(plotdata)

   scatterplot3d(plotdata2, type='h')

   It seems to me that I should not have to initialize the plotdata matrix. And
   are all those for loops really necessary? 

I remember doing similar things sometime ago simply by filling the
unwanted triangle of z with NA's.
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list