Simple question:
Why doesn't the following work? Or what 'R' rule am I missing?
tclass <- "Testing 1 2 3"
if(tclass == "Testing 1 2 3")
{
cat("Testing", tclass, "\n")
}
else
{
cat(tclass, "\n")
}
I get an error 'else' is unexpected.
Thank you.
Kevin
Because the line if... Is syntactically complete. Move the else statement to the
line above it like
tclass <- "Testing 1 2 3"
if ( tclass == "Testing 1 2 3" )
{
cat ( "Testing" , tclass , "\n" )
} else
{
cat ( tclass , "\n" )
}
david
--
David
?
-----------------------------------------------------
David Huffer, Ph.D. Senior Statistician
CSOSA/Washington, DC david.huffer at csosa.gov
-----------------------------------------------------
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of rkevinburton at charter.net
Sent: Monday, August 03, 2009 4:41 PM
To: r-help at r-project.org
Subject: [R] if confusion
Simple question:
Why doesn't the following work? Or what 'R' rule am I missing?
tclass <- "Testing 1 2 3"
if(tclass == "Testing 1 2 3")
{
cat("Testing", tclass, "\n")
}
else
{
cat(tclass, "\n")
}
I get an error 'else' is unexpected.
Thank you.
Kevin
______________________________________________
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.
This is because you could imagine a program like the following:
tclass <- "Testing 1 2 3"
if(tclass == "Testing 1 2 3")
{
cat("Testing", tclass, "\n")
}
cat("more stuff")
However, in your program, the next line is "else", which is not a
command you can just put in the middle of your program.
The solution is :
tclass <- "Testing 1 2 3"
if(tclass == "Testing 1 2 3")
{
cat("Testing", tclass, "\n")
} else {
cat(tclass, "\n")
}
Erik
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of rkevinburton at charter.net
Sent: Monday, August 03, 2009 3:41 PM
To: r-help at r-project.org
Subject: [R] if confusion
Simple question:
Why doesn't the following work? Or what 'R' rule am I missing?
tclass <- "Testing 1 2 3"
if(tclass == "Testing 1 2 3")
{
cat("Testing", tclass, "\n")
}
else
{
cat(tclass, "\n")
}
I get an error 'else' is unexpected.
Thank you.
Kevin
______________________________________________
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.
On 4/08/2009, at 8:40 AM, rkevinburton at charter.net wrote:> Simple question: > > Why doesn't the following work? Or what 'R' rule am I missing? > > tclass <- "Testing 1 2 3" > if(tclass == "Testing 1 2 3") > { > cat("Testing", tclass, "\n") > } > else > { > cat(tclass, "\n") > } > > I get an error 'else' is unexpected.The segment if(tclass == "Testing 1 2 3") { cat("Testing", tclass, "\n") } Is syntactically complete, so that when the parser comes to the ``else'' it finds it ``dangling'' and hence ``unexpected''. The following wee adjustment works: tclass <- "Testing 1 2 3" if(tclass == "Testing 1 2 3") { cat("Testing", tclass, "\n") } else { cat(tclass, "\n") } cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}