use expression() in a loop

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

use expression() in a loop

Nanye Long
Hi all,

I want to do plot() in a loop to make 10 graphs, so I have some code like

for (i in 1:10) {
   plot(... ... , xlab = expression(g[i]) )
}

I expect g_1, g_2, and so on appear on x labels, but it simply prints
g_i for each graph. Does anybody know how to get around this problem?
Thanks.

NL

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: use expression() in a loop

Sundar Dorai-Raj


Nanye Long said the following on 8/18/2008 3:00 PM:

> Hi all,
>
> I want to do plot() in a loop to make 10 graphs, so I have some code like
>
> for (i in 1:10) {
>    plot(... ... , xlab = expression(g[i]) )
> }
>
> I expect g_1, g_2, and so on appear on x labels, but it simply prints
> g_i for each graph. Does anybody know how to get around this problem?
> Thanks.
>
> NL
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Try:

par(mfrow = c(2, 5))
for (i in 1:10) {
   plot(1, 1, xlab = bquote(g[.(i)]))
}

HTH,

--sundar

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: use expression() in a loop

Marc Schwartz
In reply to this post by Nanye Long
on 08/18/2008 05:00 PM Nanye Long wrote:

> Hi all,
>
> I want to do plot() in a loop to make 10 graphs, so I have some code like
>
> for (i in 1:10) {
>    plot(... ... , xlab = expression(g[i]) )
> }
>
> I expect g_1, g_2, and so on appear on x labels, but it simply prints
> g_i for each graph. Does anybody know how to get around this problem?
> Thanks.
>
> NL

Try this:

par(mfrow = c(5, 2))

for (i in 1:10)
  plot(1, xlab = bquote(g[.(i)]))


Note the use of bquote() and the use of .(i), which replaces 'i' with
the value of i in each iteration.

See ?plotmath for some examples and ?bquote

HTH,

Marc Schwartz

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: use expression() in a loop

Henrik Bengtsson
...or use the more generic substitute() to replace parts of an expression, e.g.

  i <- 3;
  xlab1 <- substitute(g[idx], list=list(idx=i));
  xlab2 <- bquote(g[.(i)]);
  stopifnot(identical(xlab1, xlab2));

/Henrik


On Mon, Aug 18, 2008 at 3:18 PM, Marc Schwartz
<[hidden email]> wrote:

> on 08/18/2008 05:00 PM Nanye Long wrote:
>> Hi all,
>>
>> I want to do plot() in a loop to make 10 graphs, so I have some code like
>>
>> for (i in 1:10) {
>>    plot(... ... , xlab = expression(g[i]) )
>> }
>>
>> I expect g_1, g_2, and so on appear on x labels, but it simply prints
>> g_i for each graph. Does anybody know how to get around this problem?
>> Thanks.
>>
>> NL
>
> Try this:
>
> par(mfrow = c(5, 2))
>
> for (i in 1:10)
>  plot(1, xlab = bquote(g[.(i)]))
>
>
> Note the use of bquote() and the use of .(i), which replaces 'i' with
> the value of i in each iteration.
>
> See ?plotmath for some examples and ?bquote
>
> HTH,
>
> Marc Schwartz
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

paste: multiple collapse arguments?

Remko Duursma-3
In reply to this post by Marc Schwartz

Dear R-helpers,

I have a numeric vector, like:

x <- c(1,2,3,4,5,6)

I make this into a string for output to a text file, separated by \n:

paste(x, collapse="\n")

Is there a way to alternate the collapse argument? So between the first two elements of x, I want to separate by " ", then by "\n", and so forth.
The result should then look like:
"1 2\n3 4\n5 6"

(This way I get 2 elements of x on each line using writeLines, instead of one or all).
I could do this in some ugly loop, but surely there is a better way?

thanks,
Remko






_________________________________________________________________
Get thousands of games on your PC, your mobile phone, and the web with Windows®.

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: paste: multiple collapse arguments?

jholtman
Try this:

> x <- c(1,2,3,4,5,6)
> paste(paste(x[seq(1, by=2, length=length(x)/2)], x[seq(2, by=2, length=length(x)/2)]), collapse="\n")
[1] "1 2\n3 4\n5 6"
>

On Mon, Aug 25, 2008 at 7:36 PM, remko duursma <[hidden email]> wrote:

>
> Dear R-helpers,
>
> I have a numeric vector, like:
>
> x <- c(1,2,3,4,5,6)
>
> I make this into a string for output to a text file, separated by \n:
>
> paste(x, collapse="\n")
>
> Is there a way to alternate the collapse argument? So between the first two elements of x, I want to separate by " ", then by "\n", and so forth.
> The result should then look like:
> "1 2\n3 4\n5 6"
>
> (This way I get 2 elements of x on each line using writeLines, instead of one or all).
> I could do this in some ugly loop, but surely there is a better way?
>
> thanks,
> Remko
>
>
>
>
>
>
> _________________________________________________________________
> Get thousands of games on your PC, your mobile phone, and the web with Windows(R).
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>



--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: paste: multiple collapse arguments?

Moshe Olshansky-2
In reply to this post by Remko Duursma-3
One possibility is:
y <- rep(" ",6)
y[6] <- ""
y[c(2,4)] <- "\n"
res <- paste(paste(x,y,sep=""),collapse="")


--- On Tue, 26/8/08, remko duursma <[hidden email]> wrote:

> From: remko duursma <[hidden email]>
> Subject: [R] paste: multiple collapse arguments?
> To: [hidden email]
> Received: Tuesday, 26 August, 2008, 9:36 AM
> Dear R-helpers,
>
> I have a numeric vector, like:
>
> x <- c(1,2,3,4,5,6)
>
> I make this into a string for output to a text file,
> separated by \n:
>
> paste(x, collapse="\n")
>
> Is there a way to alternate the collapse argument? So
> between the first two elements of x, I want to separate by
> " ", then by "\n", and so forth.
> The result should then look like:
> "1 2\n3 4\n5 6"
>
> (This way I get 2 elements of x on each line using
> writeLines, instead of one or all).
> I could do this in some ugly loop, but surely there is a
> better way?
>
> thanks,
> Remko
>
>
>
>
>
>
> _________________________________________________________________
> Get thousands of games on your PC, your mobile phone, and
> the web with Windows®.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained,
> reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: paste: multiple collapse arguments?

Christos Hatzis
In reply to this post by Remko Duursma-3
Try this:

> paste(paste(x, c(" ","\n"), sep=""), collapse="")
[1] "1 2\n3 4\n5 6\n"

-Christos Hatzis

> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of remko duursma
> Sent: Monday, August 25, 2008 7:37 PM
> To: [hidden email]
> Subject: [R] paste: multiple collapse arguments?
>
>
> Dear R-helpers,
>
> I have a numeric vector, like:
>
> x <- c(1,2,3,4,5,6)
>
> I make this into a string for output to a text file, separated by \n:
>
> paste(x, collapse="\n")
>
> Is there a way to alternate the collapse argument? So between
> the first two elements of x, I want to separate by " ", then
> by "\n", and so forth.
> The result should then look like:
> "1 2\n3 4\n5 6"
>
> (This way I get 2 elements of x on each line using
> writeLines, instead of one or all).
> I could do this in some ugly loop, but surely there is a better way?
>
> thanks,
> Remko
>
>
>
>
>
>
> _________________________________________________________________
> Get thousands of games on your PC, your mobile phone, and the
> web with Windows..
>
> [[alternative HTML version deleted]]
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: paste: multiple collapse arguments?

Henrique Dallazuanna
In reply to this post by Remko Duursma-3
Try this also:

paste(apply(matrix(x, 2), 2, paste, collapse = ' '), collapse = "\n")

On Mon, Aug 25, 2008 at 8:36 PM, remko duursma <[hidden email]> wrote:

>
> Dear R-helpers,
>
> I have a numeric vector, like:
>
> x <- c(1,2,3,4,5,6)
>
> I make this into a string for output to a text file, separated by \n:
>
> paste(x, collapse="\n")
>
> Is there a way to alternate the collapse argument? So between the first two elements of x, I want to separate by " ", then by "\n", and so forth.
> The result should then look like:
> "1 2\n3 4\n5 6"
>
> (This way I get 2 elements of x on each line using writeLines, instead of one or all).
> I could do this in some ugly loop, but surely there is a better way?
>
> thanks,
> Remko
>
>
>
>
>
>
> _________________________________________________________________
> Get thousands of games on your PC, your mobile phone, and the web with Windows(R).
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>



--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.