Re: [R] else problem

2011-06-24 Thread David Winsemius


On Jun 24, 2011, at 7:47 AM, Petr PIKAL wrote:


Hi


Thank you for all your help! I did not know to use "" when  
searching for



help, as ?mean, etc, had always worked for me in the past.
It makes perfect sense why 'else' was causing me the trouble the  
way I

was

using it. I think it was working in my other code, despite the same
format, because it was part of a function and thus would have been
executed completely within the function?


Yes and no

if you made your function like that

fff<-function(x)
if (x>0) print("A")
else print("B")

you would get immediate error


else {

Error: unexpected 'else' in "else"

So you probably enclosed whole function into brackets like

fff<-function(x) {
if (x>0) print("A")
else print("B")
}


If you successfully get the code into a function there will be no  
error. I suspect your demonstration of the error was at the console.  
If you use Bert's "brace-enclosure" method to delay evaluation until  
the full function is entered you get a working function that has no  
braces in its body (as I showed earlier):


> {fntest <- function() if (FALSE)  print("T")
+ else  print("F") }
>  fntest
function() if (FALSE)  print("T")
else  print("F")
> fntest()
[1] "F"

--
David.



and in that case you did not experienced this else problem not  
because it

is a function but because you used those brackets.

Regards
Petr




Cheers,
Kara
____________
From: Bert Gunter [gunter.ber...@gene.com]
Sent: June 23, 2011 8:27 AM
To: David Winsemius
Cc: Kara Przeczek; r-help@r-project.org
Subject: Re: [R] else problem

Perhaps some additional clarification...  (???)


if (length(dat2f$year)%%2==0) {
md <-dat2f[, list(med_year = max(year[which(abs(tot_km3y -
median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]),

med_TotQ =

median(tot_km3y))]  }
else {


If this line is executed at a console session it will fail because  
the

interpreter does not keep a copy of the last condition. If you moved

the

closing curley-brace to just befor the 'else', you should get the

behavior

you expect (if your other code is correct):


Well, um.. not sure if this is what you meant, but what is happening
at the console is that when you type , the interpreter checks
for a syntactically complete statement. If it finds what has been
given to it **thus far** is, it tries to execute it (if not, it gives
a continuation character and waits for more input) and, as you said,
then starts anew to interpret the next line(s) entered, "forgetting"
all previous. The problem above is that the" if()" statement up to  
the

close bracket, "}" is syntactically complete, and so the "else{" that
follows makes no sense as the beginnig of a new line to  be
interpreted.

The simplest and universal solution to this is to simply enclose the
whole conditional in" { }":

{if(length ...
...
else {...}
}

This forces the interpreter to wait for the last "}" before it will
interpret and execute.

Hoping this clarifies rather than obfuscates.

-- Bert





Try instead:

if (length(dat2f$year)%%2==0) {
md <-dat2f[, list(med_year = max(year[which(abs(tot_km3y -
median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]),

med_TotQ =

median(tot_km3y))]
} else {
md <-dat2f[, list(med_year = year[which(tot_km3y ==

median(tot_km3y))],

med_TotQ = median(tot_km3y))]
}



md <-dat2f[, list(med_year = year[which(tot_km3y ==

median(tot_km3y))],

med_TotQ = median(tot_km3y))]
}

Each individual piece works perfectly on its own, but together I  
get

the

following error:


if (length(dat2f$year)%%2==0) {


+   md <-dat2f[, list(med_year = max(year[which(abs(tot_km3y -
median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]),

med_TotQ =

median(tot_km3y))] }


else {


Error: unexpected 'else' in "else"


md <-dat2f[, list(med_year = year[which(tot_km3y ==

median(tot_km3y))],

med_TotQ = median(tot_km3y))]
}


Error: unexpected '}' in "  }"




When I tried to look up "else" I got this error:


?else


Error: unexpected 'else' in "?else"


Try instead:

?"else"

--
David


I have used exactly the same set up with if...else in other code  
and

it

worked fine then. I tried to run it again, and I got the same error

as

above. What is the problem? I hope it isn't something simple and

silly!


I realize that I can use the first line:

md <- dat2f[, list(med_year = max(year[which(abs(tot_km3y -
median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]),

med_TotQ =

median(tot_km3y))]

for all data sets and it will give me the median for both odd and
even-length data sets, but it is now about the princip

Re: [R] else problem

2011-06-24 Thread Petr PIKAL
Hi
> 
> Thank you for all your help! I did not know to use "" when searching for 

> help, as ?mean, etc, had always worked for me in the past. 
> It makes perfect sense why 'else' was causing me the trouble the way I 
was
> using it. I think it was working in my other code, despite the same 
> format, because it was part of a function and thus would have been 
> executed completely within the function?

Yes and no

if you made your function like that

fff<-function(x)
if (x>0) print("A")
else print("B")

you would get immediate error

> else {
Error: unexpected 'else' in "else"

So you probably enclosed whole function into brackets like

fff<-function(x) {
if (x>0) print("A")
else print("B")
}

and in that case you did not experienced this else problem not because it 
is a function but because you used those brackets.

Regards
Petr



> Cheers,
> Kara
> 
> From: Bert Gunter [gunter.ber...@gene.com]
> Sent: June 23, 2011 8:27 AM
> To: David Winsemius
> Cc: Kara Przeczek; r-help@r-project.org
> Subject: Re: [R] else problem
> 
> Perhaps some additional clarification...  (???)
> 
> >> if (length(dat2f$year)%%2==0) {
> >>  md <-dat2f[, list(med_year = max(year[which(abs(tot_km3y -
> >> median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]), 
med_TotQ =
> >> median(tot_km3y))]  }
> >> else {
> >
> > If this line is executed at a console session it will fail because the
> > interpreter does not keep a copy of the last condition. If you moved 
the
> > closing curley-brace to just befor the 'else', you should get the 
behavior
> > you expect (if your other code is correct):
> 
> Well, um.. not sure if this is what you meant, but what is happening
> at the console is that when you type , the interpreter checks
> for a syntactically complete statement. If it finds what has been
> given to it **thus far** is, it tries to execute it (if not, it gives
> a continuation character and waits for more input) and, as you said,
> then starts anew to interpret the next line(s) entered, "forgetting"
> all previous. The problem above is that the" if()" statement up to the
> close bracket, "}" is syntactically complete, and so the "else{" that
> follows makes no sense as the beginnig of a new line to  be
> interpreted.
> 
> The simplest and universal solution to this is to simply enclose the
> whole conditional in" { }":
> 
> {if(length ...
> ...
> else {...}
> }
> 
> This forces the interpreter to wait for the last "}" before it will
> interpret and execute.
> 
> Hoping this clarifies rather than obfuscates.
> 
> -- Bert
> 
> 
> 
> >
> > Try instead:
> >
> > if (length(dat2f$year)%%2==0) {
> >  md <-dat2f[, list(med_year = max(year[which(abs(tot_km3y -
> > median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]), 
med_TotQ =
> > median(tot_km3y))]
> > } else {
> >  md <-dat2f[, list(med_year = year[which(tot_km3y == 
median(tot_km3y))],
> > med_TotQ = median(tot_km3y))]
> >  }
> >
> >
> >>  md <-dat2f[, list(med_year = year[which(tot_km3y == 
median(tot_km3y))],
> >> med_TotQ = median(tot_km3y))]
> >>  }
> >>
> >> Each individual piece works perfectly on its own, but together I get 
the
> >> following error:
> >>
> >>> if (length(dat2f$year)%%2==0) {
> >>
> >> +   md <-dat2f[, list(med_year = max(year[which(abs(tot_km3y -
> >> median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]), 
med_TotQ =
> >> median(tot_km3y))] }
> >>>
> >>> else {
> >>
> >> Error: unexpected 'else' in "else"
> >>>
> >>>  md <-dat2f[, list(med_year = year[which(tot_km3y == 
median(tot_km3y))],
> >>> med_TotQ = median(tot_km3y))]
> >>>  }
> >>
> >> Error: unexpected '}' in "  }"
> >>>
> >>
> >> When I tried to look up "else" I got this error:
> >>
> >>> ?else
> >>
> >> Error: unexpected 'else' in "?else"
> >
> > Try instead:
> >
> > ?"else"
> >
> > --
> > David
> >>
> >> I have used exactly the same set up with if...else in other code and 
it
> >> worked fine then. I tried to run it again, and I got the same error 
as
> >> above. What is the problem? I hope it isn't something simple

Re: [R] else problem

2011-06-23 Thread David Winsemius


On Jun 23, 2011, at 12:05 PM, Kara Przeczek wrote:

Thank you for all your help! I did not know to use "" when searching  
for help, as ?mean, etc, had always worked for me in the past.
It makes perfect sense why 'else' was causing me the trouble the way  
I was using it. I think it was working in my other code, despite the  
same format, because it was part of a function and thus would have  
been executed completely within the function?


Right. When done at the console or sourced, the problem will arise,  
but not when inside a function. Bert's strategy of enclosing the whole  
call in "{}" can be used to demonstrate at the console:


> {fntest <- function() if (FALSE)  print("T")
+else  print("F") }
> fntest()
[1] "F"

Or you can enclose just the body:

> fntest <- function() { if (FALSE)   print("T")
+  else { print("F") } }
> fntest()
[1] "F"
--
David



Cheers,
Kara

From: Bert Gunter [gunter.ber...@gene.com]
Sent: June 23, 2011 8:27 AM
To: David Winsemius
Cc: Kara Przeczek; r-help@r-project.org
Subject: Re: [R] else problem

Perhaps some additional clarification...  (???)


if (length(dat2f$year)%%2==0) {
md <-dat2f[, list(med_year = max(year[which(abs(tot_km3y -
median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]),  
med_TotQ =

median(tot_km3y))]  }
else {


If this line is executed at a console session it will fail because  
the
interpreter does not keep a copy of the last condition. If you  
moved the
closing curley-brace to just befor the 'else', you should get the  
behavior

you expect (if your other code is correct):


Well, um.. not sure if this is what you meant, but what is happening
at the console is that when you type , the interpreter checks
for a syntactically complete statement. If it finds what has been
given to it **thus far** is, it tries to execute it (if not, it gives
a continuation character and waits for more input) and, as you said,
then starts anew to interpret the next line(s) entered, "forgetting"
all previous. The problem above is that the" if()" statement up to the
close bracket, "}" is syntactically complete, and so the "else{" that
follows makes no sense as the beginnig of a new line to  be
interpreted.

The simplest and universal solution to this is to simply enclose the
whole conditional in" { }":

{if(length ...
...
else {...}
}

This forces the interpreter to wait for the last "}" before it will
interpret and execute.

Hoping this clarifies rather than obfuscates.

-- Bert





Try instead:

if (length(dat2f$year)%%2==0) {
md <-dat2f[, list(med_year = max(year[which(abs(tot_km3y -
median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]),  
med_TotQ =

median(tot_km3y))]
} else {
md <-dat2f[, list(med_year = year[which(tot_km3y ==  
median(tot_km3y))],

med_TotQ = median(tot_km3y))]
}


md <-dat2f[, list(med_year = year[which(tot_km3y ==  
median(tot_km3y))],

med_TotQ = median(tot_km3y))]
}

Each individual piece works perfectly on its own, but together I  
get the

following error:


if (length(dat2f$year)%%2==0) {


+   md <-dat2f[, list(med_year = max(year[which(abs(tot_km3y -
median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]),  
med_TotQ =

median(tot_km3y))] }


else {


Error: unexpected 'else' in "else"


md <-dat2f[, list(med_year = year[which(tot_km3y ==  
median(tot_km3y))],

med_TotQ = median(tot_km3y))]
}


Error: unexpected '}' in "  }"




When I tried to look up "else" I got this error:


?else


Error: unexpected 'else' in "?else"


Try instead:

?"else"

--
David


I have used exactly the same set up with if...else in other code  
and it
worked fine then. I tried to run it again, and I got the same  
error as
above. What is the problem? I hope it isn't something simple and  
silly!


I realize that I can use the first line:

md <- dat2f[, list(med_year = max(year[which(abs(tot_km3y -
median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]),  
med_TotQ =

median(tot_km3y))]

for all data sets and it will give me the median for both odd and
even-length data sets, but it is now about the principle; why  
won't the

if...else work?

Thank you very much for your time!

Kara


David Winsemius, MD
West Hartford, CT

__
R-help@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.





--
"Men by nature long to get on to the ultimate truths, and will often
be impatient with elementary studies or fight shy

Re: [R] else problem

2011-06-23 Thread Kara Przeczek
Thank you for all your help! I did not know to use "" when searching for help, 
as ?mean, etc, had always worked for me in the past. 
It makes perfect sense why 'else' was causing me the trouble the way I was 
using it. I think it was working in my other code, despite the same format, 
because it was part of a function and thus would have been executed completely 
within the function?
Cheers,
Kara

From: Bert Gunter [gunter.ber...@gene.com]
Sent: June 23, 2011 8:27 AM
To: David Winsemius
Cc: Kara Przeczek; r-help@r-project.org
Subject: Re: [R] else problem

Perhaps some additional clarification...  (???)

>> if (length(dat2f$year)%%2==0) {
>>  md <-dat2f[, list(med_year = max(year[which(abs(tot_km3y -
>> median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]), med_TotQ =
>> median(tot_km3y))]  }
>> else {
>
> If this line is executed at a console session it will fail because the
> interpreter does not keep a copy of the last condition. If you moved the
> closing curley-brace to just befor the 'else', you should get the behavior
> you expect (if your other code is correct):

Well, um.. not sure if this is what you meant, but what is happening
at the console is that when you type , the interpreter checks
for a syntactically complete statement. If it finds what has been
given to it **thus far** is, it tries to execute it (if not, it gives
a continuation character and waits for more input) and, as you said,
then starts anew to interpret the next line(s) entered, "forgetting"
all previous. The problem above is that the" if()" statement up to the
close bracket, "}" is syntactically complete, and so the "else{" that
follows makes no sense as the beginnig of a new line to  be
interpreted.

The simplest and universal solution to this is to simply enclose the
whole conditional in" { }":

{if(length ...
...
else {...}
}

This forces the interpreter to wait for the last "}" before it will
interpret and execute.

Hoping this clarifies rather than obfuscates.

-- Bert



>
> Try instead:
>
> if (length(dat2f$year)%%2==0) {
>  md <-dat2f[, list(med_year = max(year[which(abs(tot_km3y -
> median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]), med_TotQ =
> median(tot_km3y))]
> } else {
>  md <-dat2f[, list(med_year = year[which(tot_km3y == median(tot_km3y))],
> med_TotQ = median(tot_km3y))]
>  }
>
>
>>  md <-dat2f[, list(med_year = year[which(tot_km3y == median(tot_km3y))],
>> med_TotQ = median(tot_km3y))]
>>  }
>>
>> Each individual piece works perfectly on its own, but together I get the
>> following error:
>>
>>> if (length(dat2f$year)%%2==0) {
>>
>> +   md <-dat2f[, list(med_year = max(year[which(abs(tot_km3y -
>> median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]), med_TotQ =
>> median(tot_km3y))] }
>>>
>>> else {
>>
>> Error: unexpected 'else' in "else"
>>>
>>>  md <-dat2f[, list(med_year = year[which(tot_km3y == median(tot_km3y))],
>>> med_TotQ = median(tot_km3y))]
>>>  }
>>
>> Error: unexpected '}' in "  }"
>>>
>>
>> When I tried to look up "else" I got this error:
>>
>>> ?else
>>
>> Error: unexpected 'else' in "?else"
>
> Try instead:
>
> ?"else"
>
> --
> David
>>
>> I have used exactly the same set up with if...else in other code and it
>> worked fine then. I tried to run it again, and I got the same error as
>> above. What is the problem? I hope it isn't something simple and silly!
>>
>> I realize that I can use the first line:
>>
>>  md <- dat2f[, list(med_year = max(year[which(abs(tot_km3y -
>> median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]), med_TotQ =
>> median(tot_km3y))]
>>
>> for all data sets and it will give me the median for both odd and
>> even-length data sets, but it is now about the principle; why won't the
>> if...else work?
>>
>> Thank you very much for your time!
>>
>> Kara
>
> David Winsemius, MD
> West Hartford, CT
>
> __
> R-help@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.
>



--
"Men by nature long to get on to the ultimate truths, and will often
be impatient with elementary studies or fight shy of them. If it were
possible to reach the ultimate truths without the elementary studies
usually prefixed to them, these would not be preparatory studies but
superfluous diversions."

-- Maimonides (1135-1204)

Bert Gunter
Genentech Nonclinical Biostatistics

__
R-help@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.


Re: [R] else problem

2011-06-23 Thread Bert Gunter
Perhaps some additional clarification...  (???)

>> if (length(dat2f$year)%%2==0) {
>>  md <-dat2f[, list(med_year = max(year[which(abs(tot_km3y -
>> median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]), med_TotQ =
>> median(tot_km3y))]  }
>> else {
>
> If this line is executed at a console session it will fail because the
> interpreter does not keep a copy of the last condition. If you moved the
> closing curley-brace to just befor the 'else', you should get the behavior
> you expect (if your other code is correct):

Well, um.. not sure if this is what you meant, but what is happening
at the console is that when you type , the interpreter checks
for a syntactically complete statement. If it finds what has been
given to it **thus far** is, it tries to execute it (if not, it gives
a continuation character and waits for more input) and, as you said,
then starts anew to interpret the next line(s) entered, "forgetting"
all previous. The problem above is that the" if()" statement up to the
close bracket, "}" is syntactically complete, and so the "else{" that
follows makes no sense as the beginnig of a new line to  be
interpreted.

The simplest and universal solution to this is to simply enclose the
whole conditional in" { }":

{if(length ...
...
else {...}
}

This forces the interpreter to wait for the last "}" before it will
interpret and execute.

Hoping this clarifies rather than obfuscates.

-- Bert



>
> Try instead:
>
> if (length(dat2f$year)%%2==0) {
>  md <-dat2f[, list(med_year = max(year[which(abs(tot_km3y -
> median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]), med_TotQ =
> median(tot_km3y))]
> } else {
>  md <-dat2f[, list(med_year = year[which(tot_km3y == median(tot_km3y))],
> med_TotQ = median(tot_km3y))]
>  }
>
>
>>  md <-dat2f[, list(med_year = year[which(tot_km3y == median(tot_km3y))],
>> med_TotQ = median(tot_km3y))]
>>  }
>>
>> Each individual piece works perfectly on its own, but together I get the
>> following error:
>>
>>> if (length(dat2f$year)%%2==0) {
>>
>> +   md <-dat2f[, list(med_year = max(year[which(abs(tot_km3y -
>> median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]), med_TotQ =
>> median(tot_km3y))] }
>>>
>>> else {
>>
>> Error: unexpected 'else' in "else"
>>>
>>>  md <-dat2f[, list(med_year = year[which(tot_km3y == median(tot_km3y))],
>>> med_TotQ = median(tot_km3y))]
>>>  }
>>
>> Error: unexpected '}' in "  }"
>>>
>>
>> When I tried to look up "else" I got this error:
>>
>>> ?else
>>
>> Error: unexpected 'else' in "?else"
>
> Try instead:
>
> ?"else"
>
> --
> David
>>
>> I have used exactly the same set up with if...else in other code and it
>> worked fine then. I tried to run it again, and I got the same error as
>> above. What is the problem? I hope it isn't something simple and silly!
>>
>> I realize that I can use the first line:
>>
>>  md <- dat2f[, list(med_year = max(year[which(abs(tot_km3y -
>> median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]), med_TotQ =
>> median(tot_km3y))]
>>
>> for all data sets and it will give me the median for both odd and
>> even-length data sets, but it is now about the principle; why won't the
>> if...else work?
>>
>> Thank you very much for your time!
>>
>> Kara
>
> David Winsemius, MD
> West Hartford, CT
>
> __
> R-help@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.
>



-- 
"Men by nature long to get on to the ultimate truths, and will often
be impatient with elementary studies or fight shy of them. If it were
possible to reach the ultimate truths without the elementary studies
usually prefixed to them, these would not be preparatory studies but
superfluous diversions."

-- Maimonides (1135-1204)

Bert Gunter
Genentech Nonclinical Biostatistics

__
R-help@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.


Re: [R] else problem

2011-06-23 Thread David Winsemius


On Jun 23, 2011, at 10:59 AM, Kara Przeczek wrote:


Dear R users,
I have run into a problem using if...else and I hope you can shed  
some light on it. I am using R version 2.2.0.1.


I have the following data frame:

head(dat2f)

year  tot_km3y
[1,] 1964 0.1876854
[2,] 1965 0.1835116
[3,] 1966 0.1915012
[4,] 1967 0.1869758
[5,] 1968 0.2249865
[6,] 1969 0.1916011

I need to pick out the median year, and since there are an even  
number of data, I cannot use 'median' directly since it gives me a  
non-existent year/discharge. I found a way to get around that with  
the following:


 md <- dat2f[, list(med_year = max(year[which(abs(tot_km3y -  
median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]),  
med_TotQ = median(tot_km3y))]
(I really only need the year, not the actual discharge with that  
year, which is why I left med_TotQ as the true median)


However, I have some data sets that have an odd number of data for  
which the following works perfectly:


 md <-dat2f[, list(med_year = year[which(tot_km3y ==  
median(tot_km3y))], med_TotQ = median(tot_km3y))]


Thus, I would like to apply the above calculations depended on the  
condition of:


length(dat2f$year)%%2==0

I put it all together as below:

if (length(dat2f$year)%%2==0) {
 md <-dat2f[, list(med_year = max(year[which(abs(tot_km3y -  
median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]),  
med_TotQ = median(tot_km3y))]  }

else {


If this line is executed at a console session it will fail because the  
interpreter does not keep a copy of the last condition. If you moved  
the closing curley-brace to just befor the 'else', you should get the  
behavior you expect (if your other code is correct):


Try instead:

if (length(dat2f$year)%%2==0) {
 md <-dat2f[, list(med_year = max(year[which(abs(tot_km3y -  
median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]),  
med_TotQ = median(tot_km3y))]

} else {
 md <-dat2f[, list(med_year = year[which(tot_km3y ==  
median(tot_km3y))], med_TotQ = median(tot_km3y))]

 }


 md <-dat2f[, list(med_year = year[which(tot_km3y ==  
median(tot_km3y))], med_TotQ = median(tot_km3y))]

 }

Each individual piece works perfectly on its own, but together I get  
the following error:



if (length(dat2f$year)%%2==0) {
+   md <-dat2f[, list(med_year = max(year[which(abs(tot_km3y -  
median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]),  
med_TotQ = median(tot_km3y))] }

else {

Error: unexpected 'else' in "else"
 md <-dat2f[, list(med_year = year[which(tot_km3y ==  
median(tot_km3y))], med_TotQ = median(tot_km3y))]

 }

Error: unexpected '}' in "  }"




When I tried to look up "else" I got this error:


?else

Error: unexpected 'else' in "?else"


Try instead:

?"else"

--
David


I have used exactly the same set up with if...else in other code and  
it worked fine then. I tried to run it again, and I got the same  
error as above. What is the problem? I hope it isn't something  
simple and silly!


I realize that I can use the first line:

 md <- dat2f[, list(med_year = max(year[which(abs(tot_km3y -  
median(tot_km3y)) == min(abs(tot_km3y - median(tot_km3y ]),  
med_TotQ = median(tot_km3y))]


for all data sets and it will give me the median for both odd and  
even-length data sets, but it is now about the principle; why won't  
the if...else work?


Thank you very much for your time!

Kara


David Winsemius, MD
West Hartford, CT

__
R-help@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.