Greetings, I am recoding a dummy variable (coded 1,0) so that 0 = 2. I am using the line sciach$dummyba[sciach$ba==0] <- 2 I notice that it creates a new column dummyba, with 0 coded as 2 but with 1's now coded as NA. Is there a simple way around this in the line I am using, or do I need to have an additional line sciach$dummyba[sciach$ba==1] <- 1 Thanks in advance. David
Use ifelse: sciach$dummyba <- ifelse(sciach$ba == 0, 2, 1) On Thu, Oct 22, 2009 at 2:37 PM, David Kaplan <dkaplan at education.wisc.edu> wrote:> Greetings, > > I am recoding a dummy variable (coded 1,0) so that 0 = 2. ?I am using the > line > > sciach$dummyba[sciach$ba==0] <- 2 > > I notice that it creates a new column dummyba, with 0 coded as 2 but with > 1's now coded as NA. ?Is there a simple way around this in the line I am > using, or do I need to have an additional line > > sciach$dummyba[sciach$ba==1] <- 1 > > Thanks in advance. > > > David > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
David Kaplan wrote:> Greetings, > > I am recoding a dummy variable (coded 1,0) so that 0 = 2. I am using > the line > > sciach$dummyba[sciach$ba==0] <- 2 > > I notice that it creates a new column dummyba, with 0 coded as 2 but > with 1's now coded as NA. Is there a simple way around this in the line > I am using, or do I need to have an additional line > > sciach$dummyba[sciach$ba==1] <- 1 > > Thanks in advance. > > > DavidTry sciach$dummyba <- ifelse(sciach$ba==0,2,1) -- Kevin E. Thorpe Biostatistician/Trialist, Knowledge Translation Program Assistant Professor, Dalla Lana School of Public Health University of Toronto email: kevin.thorpe at utoronto.ca Tel: 416.864.5776 Fax: 416.864.3016
On 10/22/2009 12:37 PM, David Kaplan wrote:> Greetings, > > I am recoding a dummy variable (coded 1,0) so that 0 = 2. I am using > the line > > sciach$dummyba[sciach$ba==0] <- 2 > > I notice that it creates a new column dummyba, with 0 coded as 2 but > with 1's now coded as NA. Is there a simple way around this in the line > I am using, or do I need to have an additional line > > sciach$dummyba[sciach$ba==1] <- 1You might do the recoding like this: with(sciach, ifelse(ba == 0, 2, ifelse(ba == 1, 1, NA))) or like this: sciach$ba * -1 + 2> Thanks in advance. > > David > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894