[R] naming things in functions

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Sat Jun 22 08:40:11 CEST 2002


See the Programmers' Niche in the latest R-news (released yesterday),
which covers this very problem.

On Fri, 21 Jun 2002, Deborah S. Rocha wrote:

> Hello,
>
> 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]],
> deparse(substitute(clusters)))
>         numrows = length(rnames)
>         numcols = dim(result)[2]
>         }
> res = matrix(result, nrow = numrows, ncol = numcols, dimnames =
> list(rnames, NULL))
> return(res)
> }
>
> Some actual output of SummaryMat:
>
> $pL
>                  [,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
>
>
> --
> ...........................................
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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