What's the neat way to create a dummy from a list? The code below is not replicable, but hopefully self-explanatory... d$treatment<-rep(1,length(d)) notreat<-c("AR", "DE", "MS", "NY", "TN", "AK", "LA", "MD", "NC", "OK", "UT", "VA") #i would really like this to work: d$treatment[d$st==any(notreat)]<-0 #but instead i resort to this for (i in 1:length(notreat)) { temp.st <- notreat[i] d$treatment[d$st==temp.st]<-0 i<-i+1 } Thanks, list!
well, you can first define a factor, and the use model.matrix(), e.g., fc <- factor(c("AR", "DE", "MS", "NY")) model.matrix(~ fc) for more info check ?model.matrix(), e.g., if you want to change the contrasts. I hope it helps. Best, Dimitris Rob Denniker wrote:> What's the neat way to create a dummy from a list? > The code below is not replicable, but hopefully self-explanatory... > > d$treatment<-rep(1,length(d)) > > notreat<-c("AR", "DE", "MS", "NY", "TN", "AK", "LA", "MD", "NC", "OK", "UT", "VA") > > #i would really like this to work: > d$treatment[d$st==any(notreat)]<-0 > > #but instead i resort to this > for (i in 1:length(notreat)) { > temp.st <- notreat[i] > d$treatment[d$st==temp.st]<-0 > i<-i+1 } > > > Thanks, list! > > ______________________________________________ > 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. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
On 3/23/2009 5:44 AM, Rob Denniker wrote:> What's the neat way to create a dummy from a list? > The code below is not replicable, but hopefully self-explanatory... > > d$treatment<-rep(1,length(d)) > > notreat<-c("AR", "DE", "MS", "NY", "TN", "AK", "LA", "MD", "NC", "OK", "UT", "VA") > > #i would really like this to work: > d$treatment[d$st==any(notreat)]<-0 > > #but instead i resort to this > for (i in 1:length(notreat)) { > temp.st <- notreat[i] > d$treatment[d$st==temp.st]<-0 > i<-i+1 }d$treatment <- as.numeric(!(d$st %in% notreat))> Thanks, list! > > ______________________________________________ > 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