VAR <- cut(OTHER, c(min(OTHER), tau1, tau2, tau3, max(OTHER)),
labels=FALSE)
If you want to split it, use
Split(x, cut(x, levels)) and a list of each partition is returned.
Ivan
-----Original Message-----
From: gov2001-l-admin(a)fas.harvard.edu
[mailto:gov2001-l-admin@fas.harvard.edu] On Behalf Of Olivia Lau
Sent: Wednesday, April 09, 2003 4:44 PM
To: Kosuke Imai; dhopkins(a)fas.harvard.edu
Cc: gov2001-l(a)fas.harvard.edu
Subject: Re: [gov2001-l] Creating New Variables in R
Here's an easier solution for those of us who don't know what to
do with factors in R (like me):
Let's say VAR takes on a value from 2 when some logical
operation on other variables in your dataset (OTHER) holds TRUE.
So you set up four vectors of length(Y), and say:
a <- OTHER < tau1
b <- OTHER < tau2
c <- OTHER < tau3
d <- OTHER >= tau3
VAR <- a + b + c + d
Then VAR takes on the value 2 where (OTHER >= tau1 and OTHER <
tau2). You can then bind VAR to your dataframe.
Yours, Olivia.
----- Original Message -----
From: "Kosuke Imai" <kimai(a)fas.harvard.edu>
To: <dhopkins(a)fas.harvard.edu>
Cc: <gov2001-l(a)fas.harvard.edu>
Sent: Wednesday, April 09, 2003 11:00 AM
Subject: Re: [gov2001-l] Creating New Variables in R
Try using as.factor(). For example,
b<-(as.factor(a)=="2")*1
Kosuke
On Wed, 9 Apr 2003 dhopkins(a)fas.harvard.edu wrote:
> Dear All,
>
> Quick query: let's say I have a discrete, ordered variable
(VAR) that
takes on
> values 1, 2, 3, or 4. I want to use R to create
a dummy
variable for those
> observations that take on value 2. Up until now,
I have
been subsetting the
> dataset into two pieces, defined by VAR==2 and !
VAR==2,
then creating a new
> variable (DATA$NEWVAR <- 1 for the first
subset, DATA$NEWVAR
<- 0 for the
> second), and then putting the new datasets back
together.
There must be a more
> efficient way to do this, and I was curious if
anyone had
any hints as to what
it was.
Thanks a lot!
Best,
Dan
--
PhD Candidate in Government
Proctor
Harvard University
(617) 493-8402
_______________________________________________
gov2001-l mailing list
gov2001-l(a)fas.harvard.edu
http://www.fas.harvard.edu/mailman/listinfo/gov2001-l
_______________________________________________
gov2001-l mailing list
gov2001-l(a)fas.harvard.edu
http://www.fas.harvard.edu/mailman/listinfo/gov2001-l
_______________________________________________
gov2001-l mailing list
gov2001-l(a)fas.harvard.edu
http://www.fas.harvard.edu/mailman/listinfo/gov2001-l