Hey, Rers I am new to R and this question may has been asked many times or a very old one.?I have checked the archive but found nothing (maybe I used?the wrong key words).?any advise is appreciated the question is: I try to replace a col vector with some other values, see edu<-matrix(0,1000,2) edu1<-runif(1000,0,1) edu[,1]<-edu1 the colum ?vector?edu[,1]? would?have value between 0 and 1. if I want to replace the value of edu[,2] to 1 if edu[,1] less than 0.5, how to do it in the form of matrix but not?under a?loop structure? I have tried: if (edu[,1]>=0.5) edu[,2]=1 there is only the first 0.5 have been done but not all the others. thanks for any kind answer. Nan from Montreal
Hi Nan, See ?ifelse edu[,2] <- ifelse(edu[,1] < 0.5, 1, 2) edu HTH, Jorge On Mon, Aug 2, 2010 at 9:16 PM, Hey Sky <> wrote:> Hey, Rers > > I am new to R and this question may has been asked many times or a very old > one. I have checked > > the archive but found nothing (maybe I used the wrong key words). any > advise is > appreciated > > the question is: > > I try to replace a col vector with some other values, see > > edu<-matrix(0,1000,2) > > edu1<-runif(1000,0,1) > edu[,1]<-edu1 > > the colum vector edu[,1] would have value between 0 and 1. if I want to > replace the value of edu[,2] > > to 1 if edu[,1] less than 0.5, how to do it in the form of matrix but > not under > a loop structure? > > I have tried: > > if (edu[,1]>=0.5) edu[,2]=1 > > there is only the first 0.5 have been done but not all the others. thanks > for > any kind answer. > > Nan > from Montreal > > > > ______________________________________________ > 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]]
On Aug 2, 2010, at 9:16 PM, Hey Sky wrote:> Hey, Rers > > I am new to R and this question may has been asked many times or a > very old > one. I have checked > > the archive but found nothing (maybe I used the wrong key words). > any advise is > appreciated > > the question is: > > I try to replace a col vector with some other values, see > > edu<-matrix(0,1000,2) > > edu1<-runif(1000,0,1) > edu[,1]<-edu1 > > the colum vector edu[,1] would have value between 0 and 1. if I > want to > replace the value of edu[,2] > > to 1 if edu[,1] less than 0.5, how to do it in the form of matrix > but not under > a loop structure? > > I have tried: > > if (edu[,1]>=0.5) edu[,2]=1You have discovered that is not the right way and if you look at the help page for "if" you should see why (it doesn't vectorize). Try: edu[ edu[,1] < 0.5 ,1] <- 1 > min(edu[,1]) [1] 0.5001459 Or you could use ifelse, but I prefer vectorized solutions when they are clear. -- David.> > there is only the first 0.5 have been done but not all the others. > thanks for > any kind answer. > > Nan > from Montreal > > > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
Here's one way: edu[edu[, 1] < 0.5, 2] <- 1> set.seed(1) > edu<-matrix(0,1000,2) > edu1<-runif(1000,0,1) > edu[,1]<-edu1 > head(edu)[,1] [,2] [1,] 0.2655087 0 [2,] 0.3721239 0 [3,] 0.5728534 0 [4,] 0.9082078 0 [5,] 0.2016819 0 [6,] 0.8983897 0> edu[edu[, 1] < 0.5, 2] <- 1 > head(edu)[,1] [,2] [1,] 0.2655087 1 [2,] 0.3721239 1 [3,] 0.5728534 0 [4,] 0.9082078 0 [5,] 0.2016819 1 [6,] 0.8983897 0>Steven McKinney> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Hey Sky > Sent: August-02-10 6:17 PM > To: r-help at r-project.org > Subject: [R] how to do a IF ELSE in a matrix format > > Hey, Rers > > I am new to R and this question may has been asked many times or a very old > one.?I have checked > > the archive but found nothing (maybe I used?the wrong key words).?any advise is > appreciated > > the question is: > > I try to replace a col vector with some other values, see > > edu<-matrix(0,1000,2) > > edu1<-runif(1000,0,1) > edu[,1]<-edu1 > > the colum ?vector?edu[,1]? would?have value between 0 and 1. if I want to > replace the value of edu[,2] > > to 1 if edu[,1] less than 0.5, how to do it in the form of matrix but not?under > a?loop structure? > > I have tried: > > if (edu[,1]>=0.5) edu[,2]=1 > > there is only the first 0.5 have been done but not all the others. thanks for > any kind answer. > > Nan > from Montreal > > > > ______________________________________________ > 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.