[R] Question of programming style

s-luppescu@uchicago.edu s-luppescu at uchicago.edu
Sat Jul 8 00:56:10 CEST 2000


This is really a question of how to program this *BETTER*. It works as I have
done it, but is quite ugly. 

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
code:

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? 

Thanks in advance for the help.
______________________________________________________________________
Stuart Luppescu         -=-=-  University of Chicago
$(B:MJ8$HCRF`H~$NIc(B        -=-=-  s-luppescu at uchicago.edu
http://www.consortium-chicago.org/people/sl/sl.html
PGP Public Key: www.consortium-chicago.org/people/sl/pubkey.asc
ICQ #21172047  AIM: psycho7070
Swipple's Rule of Order:
        He who shouts the loudest has the floor.
>> Sent on 07-Jul-2000 at 17:45:09 with xfmail
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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