Ein eingebundener Text mit undefiniertem Zeichensatz wurde abgetrennt. Name: nicht verf?gbar URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130513/fe848ce7/attachment.pl>
Hi, ?rgb HTH Pascal 2013/5/13 David Studer <studerov@gmail.com>> Hello everybody, > > I have three variables "blue", "green" and "red" containing values 0 (no) > and 1 (yes). > > How can I easily create another variable "colors" with the values "blue", > "green" and "red"? > > I hope that you can understand my question and appreciate any solutions or > hints! > > Thank you! > David > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Gabor Grothendieck
2013-May-13 15:39 UTC
[R] reduce three columns to one with the colnames
On Mon, May 13, 2013 at 10:24 AM, David Studer <studerov at gmail.com> wrote:> Hello everybody, > > I have three variables "blue", "green" and "red" containing values 0 (no) > and 1 (yes). > > How can I easily create another variable "colors" with the values "blue", > "green" and "red"? >Suppose blue <- c(1, 0, 0, 1) green <- c(0, 0, 1, 0) red <- c(0, 1, 0, 0) Here are a few possibilities: # 1 factor(blue + 2 * green + 3 * red, labels = c("blue", "green", "red")) # 2 paste0( ifelse(blue, "blue", ""), ifelse(green, "green", ""), ifelse(red, "red", "") ) # 3 newvar <- character(length(blue)) newvar[!!blue] <- "blue" newvar[!!green] <- "green" newvar[!!red] <- "red" newvar # 4 c("blue", "green", "red")[blue + 2 * red + 3 * green] # 5 library(car) recode(blue + 2 * green + 3 * red, "1='blue'; 2='green'; 3='red'") -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com