[R] naming things in functions

Deborah S. Rocha stungis at lanl.gov
Fri Jun 21 23:35:53 CEST 2002


I'm working with R version 1.5.0 in Windows. I've written a function
(SummaryMat, segment below) which uses a loop to
repeatedly call another function (PercentsMat, segment below).

PercentsMat creates a matrix and adds rows to it each time it is called.
I use deparse(substitute(...)) to get the names of the lists sent to
PercentsMat to use them as row names in the generated matrix. When
called from the command line, PercentsMat works well.

The problem is that when PercentsMat is called from within a loop in
SummaryMat, where list names are indexed, the rows in the generated
matrix are labeled with the index name rather than its value. That is,
for example, I'm ending up with ri, ri, ri rather than r1, r2, r3.

SummaryMat <- function(datamat, numclusts, runs, fzmeasure)

for( j in numclusts )
        for( i in 1:runs )  {
                k = k+1
                sepclusters[k] = list( ClusterSep(clusters[[k]]) )  #
separate data into individual clusters
                names(sepclusters)[k] = paste(
deparse(substitute(datamat)), j, ".", i, sep = "")

                pL = PercentsMat( sepclusters[[k]], pL, all )

PercentsMat <- function(clusters, percentsmatrix, ltype){
        result =  rbind( percentsmatrix, newpercents )
        rnames = c(dimnames(percentsmatrix)[[1]],
        numrows = length(rnames)
        numcols = dim(result)[2]
res = matrix(result, nrow = numrows, ncol = numcols, dimnames =
list(rnames, NULL))

Some actual output of SummaryMat:

                 [,1] [,2] [,3]
sepclusters[[k]]  100    0    0
sepclusters[[k]]    0    0  100
sepclusters[[k]]   45   55    0

I would be very grateful if you could tell me how to get the names of
the respective "sepclusters", rather than this less meaningful output.

Thank you,

Deborah S. Rocha
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