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: "Padhye, Nikhil S" <Nikhil.S.Padhye@uth.tmc.edu>
Date: Tue, 9 Oct 2007 12:32:21 -0500
In-reply-to: <357352.46088.qm@web62003.mail.re1.yahoo.com>
References: <357352.46088.qm@web62003.mail.re1.yahoo.com>
Thread-index: AcgKgTXca8zmj0tjSFesQP7T5SRJsAAGFRQQ
Thread-topic: [S] switch keyword
Carol,
 
Not sure about switch, but
 
i.cat <- ifelse(i >= 50, 3, ifelse(i >= 20, 2, 1))
 
should do the task. Here i is your integer vector and i.cat maps those integers to the set {1,2,3}.
 
If you want i.cat to be a factor variable, enclose the right hand side _expression_ in as.factor(...).
 
Nikhil
________________________________________________

N. S. Padhye, Ph.D.

Research Assistant Professor, Biostatistician

The University of Texas Health Science Center at Houston

http://www.uth.tmc.edu



From: s-news-owner@lists.biostat.wustl.edu [mailto:s-news-owner@lists.biostat.wustl.edu] On Behalf Of carol white
Sent: Tuesday, October 09, 2007 9:09 AM
To: s-news@lists.biostat.wustl.edu
Subject: [S] switch keyword

Hi,
I have an integer vector that I want to convert to a categorical vector with values 1, 2, 3.
If the value of an element of the integer vector < 20, then, the value of the corresponding element in the categorical vector will be 1
if the value of an element of the integer vector > 20 and < 50, then, the value of the corresponding element in the categorical vector will be 2
if the value of an element of the integer vector > 50, then, the value of the corresponding element in the categorical vector will be 3

can I do this by switch and how

look forward to your feedback

carol


Catch up on fall's hot new shows on Yahoo! TV. Watch previews, get listings, and more!
<Prev in Thread] Current Thread [Next in Thread>