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