Dear R-helpers, I have dataset (data.frame) like below, x1 x2 x3 x4 x5 x6 x7 x8 x9 ... x1200 0 0 0 1 1 0 0 1 1 1 0 0 1 1 0 0 1 1 0 1 0 1 1 0 0 1 1 1 1 0 1 1 0 0 1 1 ... How can I change automatically 0=no and 1=yes. Thank you very much in advance. Kindly regards, Muhammad Subianto
> x <- data.frame(matrix(c(1,0,1,0,1,1),nrow=3))> x[x==0] <- 'no' > x[x==1] <- 'yes' > x X1 X2 1 yes no 2 no yes 3 yes yes > On Jun 15, 2005, at 9:58 AM, Muhammad Subianto wrote:> Dear R-helpers, > I have dataset (data.frame) like below, > x1 x2 x3 x4 x5 x6 x7 x8 x9 ... x1200 > 0 0 0 1 1 0 0 1 1 > 1 0 0 1 1 0 0 1 1 > 0 1 0 1 1 0 0 1 1 > 1 1 0 1 1 0 0 1 1 > ... > How can I change automatically 0=no and 1=yes. > > Thank you very much in advance. > Kindly regards, > Muhammad Subianto > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
try this: dat <- data.frame(matrix(sample(0:1, 100 * 20, TRUE), 100, 20)) ############ dat[] <- lapply(dat, factor, levels = c(0, 1), labels = c("no", "yes")) dat I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Muhammad Subianto" <subianto at gmail.com> To: <R-help at stat.math.ethz.ch> Sent: Wednesday, June 15, 2005 3:58 PM Subject: [R] how to change automatically 0=no and 1=yes> Dear R-helpers, > I have dataset (data.frame) like below, > x1 x2 x3 x4 x5 x6 x7 x8 x9 ... x1200 > 0 0 0 1 1 0 0 1 1 > 1 0 0 1 1 0 0 1 1 > 0 1 0 1 1 0 0 1 1 > 1 1 0 1 1 0 0 1 1 > ... > How can I change automatically 0=no and 1=yes. > > Thank you very much in advance. > Kindly regards, > Muhammad Subianto > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
On Wed, 2005-06-15 at 15:58 +0200, Muhammad Subianto wrote:> Dear R-helpers, > I have dataset (data.frame) like below, > x1 x2 x3 x4 x5 x6 x7 x8 x9 ... x1200 > 0 0 0 1 1 0 0 1 1 > 1 0 0 1 1 0 0 1 1 > 0 1 0 1 1 0 0 1 1 > 1 1 0 1 1 0 0 1 1 > ... > How can I change automatically 0=no and 1=yes. > > Thank you very much in advance. > Kindly regards, > Muhammad SubiantoThe easiest might be to use the following, presuming that your data frame is called 'df' and all entries are 0/1:> dfV1 V2 V3 V4 V5 V6 V7 V8 V9 V10 1 1 0 1 1 1 1 0 1 1 0 2 1 0 1 0 0 0 1 1 0 0 3 0 0 1 0 1 1 1 1 1 0 4 0 0 0 0 1 1 0 1 0 0 5 1 1 1 1 0 1 0 1 1 0 6 1 0 1 1 1 1 0 1 1 1 7 0 1 1 1 0 0 1 0 1 0 8 1 1 1 1 0 0 1 1 0 0 9 1 0 1 1 1 0 1 0 1 0 10 1 0 0 1 1 1 1 1 0 1 # Use ifelse(). By default that will return a # character matrix, so coerce back to a data # frame. Note that the entries are "factors"> as.data.frame(ifelse(df == 0, "No", "Yes"))V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 1 Yes No Yes Yes Yes Yes No Yes Yes No 2 Yes No Yes No No No Yes Yes No No 3 No No Yes No Yes Yes Yes Yes Yes No 4 No No No No Yes Yes No Yes No No 5 Yes Yes Yes Yes No Yes No Yes Yes No 6 Yes No Yes Yes Yes Yes No Yes Yes Yes 7 No Yes Yes Yes No No Yes No Yes No 8 Yes Yes Yes Yes No No Yes Yes No No 9 Yes No Yes Yes Yes No Yes No Yes No 10 Yes No No Yes Yes Yes Yes Yes No Yes See ?ifelse for more information. HTH, Marc Schwartz
Dear all, Sean Davis, Dimitris Rizopoulos and Marc Schwartz, thanks for your great help. It works perfectly. Thanks a lot. All the best, Muhammad Subianto On this day 6/15/2005 4:06 PM, Sean Davis wrote: > > x <- data.frame(matrix(c(1,0,1,0,1,1),nrow=3)) > > x[x==0] <- 'no' > > x[x==1] <- 'yes' > > x > X1 X2 > 1 yes no > 2 no yes > 3 yes yes > On this day 6/15/2005 4:06 PM, Dimitris Rizopoulos wrote: > try this: > > dat <- data.frame(matrix(sample(0:1, 100 * 20, TRUE), 100, 20)) > ############ > dat[] <- lapply(dat, factor, levels = c(0, 1), labels = c("no", > "yes")) > dat > On this day 6/15/2005 4:16 PM, Marc Schwartz wrote: > > >>as.data.frame(ifelse(df == 0, "No", "Yes")) > On this day 6/15/2005 3:58 PM, Muhammad Subianto wrote:> Dear R-helpers, > I have dataset (data.frame) like below, > x1 x2 x3 x4 x5 x6 x7 x8 x9 ... x1200 > 0 0 0 1 1 0 0 1 1 > 1 0 0 1 1 0 0 1 1 > 0 1 0 1 1 0 0 1 1 > 1 1 0 1 1 0 0 1 1 > ... > How can I change automatically 0=no and 1=yes. > > Thank you very much in advance. > Kindly regards, > Muhammad Subianto > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >