s-news
[Top] [All Lists]

Re: switch keyword

To: "'carol white'" <wht_crl@yahoo.com>, <s-news@lists.biostat.wustl.edu>
Subject: Re: switch keyword
From: "Alan Hochberg" <alan.hochberg@prosanos.com>
Date: Tue, 9 Oct 2007 10:46:27 -0400
In-reply-to: <357352.46088.qm@web62003.mail.re1.yahoo.com>
Thread-index: AcgKgSlM2xxr2AnjS/WnKDFpKm9L2gAAS9aw

Hi Carol,

 

You haven’t specified how to handle the boundary cases of a number that is exactly equal to 20 or exactly equal to 50, so I made an arbitrary choice there.  True, switch()  will work, but I think ifelse() is both clear and concise for your purposes:

 

 

> carolsVector <- c(-5,100,20,31,3,50,75,22,999)

> carolsCategory <- function(x) { ifelse(x <= 20,1,ifelse(x <= 50,2,3))}

> carolsCategory(carolsVector)

[1] 1 3 1 2 1 2 3 2 3

> 

 

Hope this helps,

 

Alan

 

Alan Hochberg

VP, Research

ProSanos Corporation

225 Market St. Ste. 502,

Harrisburg, PA 17101

Tel 717-635-2124 * Fax 717-635-2575

 

<Prev in Thread] Current Thread [Next in Thread>