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
use expression() in a loop
9 messages · Nanye Long, Sundar Dorai-Raj, Marc Schwartz +6 more
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
______________________________________________ R-help at r-project.org 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
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
...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
<marc_schwartz at comcast.net> 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
______________________________________________ R-help at r-project.org 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.
6 days later
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20080825/21dc3323/attachment.pl>
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 <remkoduursma at hotmail.com> 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]]
______________________________________________
R-help at r-project.org 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?
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 <remkoduursma at hotmail.com> wrote:
From: remko duursma <remkoduursma at hotmail.com> Subject: [R] paste: multiple collapse arguments? To: r-help at r-project.org 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]] ______________________________________________ R-help at r-project.org 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 this:
paste(paste(x, c(" ","\n"), sep=""), collapse="")
[1] "1 2\n3 4\n5 6\n" -Christos Hatzis
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of remko duursma Sent: Monday, August 25, 2008 7:37 PM To: r-help at r-project.org 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]]
Try this also: paste(apply(matrix(x, 2), 2, paste, collapse = ' '), collapse = "\n")
On Mon, Aug 25, 2008 at 8:36 PM, remko duursma <remkoduursma at hotmail.com> 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]]
______________________________________________
R-help at r-project.org 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