Re: [R] saving a character vector

From: John Fox <jfox_at_mcmaster.ca>
Date: Sun 05 Feb 2006 - 11:08:11 EST


Dear Dominik,

Replacing cat() with return() won't do the trick, I think: First, it will return only the first time through the inner loop; second, it will call return with several values.

I think that Taka probably had in mind something like:

generate.index<-function(n.item){

    result <- rep("", n.item*(n.item-1)/2)     index <- 0
    for (i in 1:(n.item - 1))

        {
            for (j in ((i+1):n.item))
                {
                index <- index + 1                
                result[index] <- paste("i", formatC(i, digits=2, flag="0"),
".",
                    formatC(j, digits=2, flag="0"), sep="")
                }
        }

    result
    }

For example:

> generate.index(4)

[1] "i001.002" "i001.003" "i001.004" "i002.003" "i002.004" "i003.004"

Note that I corrected the range of the outer (i) loop.

I hope this helps,
 John



John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox

> -----Original Message-----
> From: r-help-bounces@stat.math.ethz.ch
> [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Dominik Heier
> Sent: Saturday, February 04, 2006 6:57 PM
> To: Taka Matzmoto
> Cc: r-help@stat.math.ethz.ch
> Subject: Re: [R] saving a character vector
>
> Am Samstag, den 04.02.2006, 17:46 -0600 schrieb Taka Matzmoto:
> > Hi R users
> >
> > I wrote a function that generates some character strings.
> >
> > generate.index<-function(n.item){
> > for (i in 1:n.item)
> > {
> > for (j in ((i+1):n.item))
> > {
> >
> >
> cat("i",formatC(i,digits=2,flag="0"),".",formatC(j,digits=2,flag="0"),
> > "\n",sep="")

> ^
> replace "cat" with "return"
> > }
> >
> > }
> > }
> >
> > I like to save what appears on the screen when I run using
> > generate.index(10) as a character vector
> >
> > I used
> > temp <- generate.index(10)
> >
> > but it didn't work.
> >
> > Could you provide some advice on this issue?
> >
> > Thanks in advance
> >
> > TM
> >
> > ______________________________________________
> > R-help@stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sun Feb 05 11:15:37 2006

This archive was generated by hypermail 2.1.8 : Sun 05 Feb 2006 - 14:31:18 EST