[R] else problem

David Winsemius dwinsemius at comcast.net
Thu Jun 23 17:09:22 CEST 2011


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



More information about the R-help mailing list