Hi All, this could be a simple question but I am looking into modifying a data frame using a "condition" without the need to loop over that data, would that be possible? I have tried the following> x<-c(4,5,6,6,8) > y<-c("a","b","b","b","c") > data<-data.frame(x,y) > datax y 1 4 a 2 5 b 3 6 b 4 6 b 5 8 c if (data$x==6){ data$x<-66 } Warning message: In if (data$x == 6) { : the condition has length > 1 and only the first element will be used>I woul dlike to change the 6 in column x into 66 why the above did not work? Thanks -- View this message in context: http://www.nabble.com/Problem-with-modifying-a-data-frame-tp24922814p24922814.html Sent from the R help mailing list archive at Nabble.com.
Hi,> this could be a simple question but I am looking into modifying a data frame > using a "condition" without the need to loop over that data, would that be > possible? > > I have tried the following > >> x<-c(4,5,6,6,8) >> y<-c("a","b","b","b","c") >> data<-data.frame(x,y) >> data > x y > 1 4 a > 2 5 b > 3 6 b > 4 6 b > 5 8 c > if (data$x==6){ > data$x<-66 > } > Warning message: > In if (data$x == 6) { : > the condition has length > 1 and only the first element will be useddata[which(data$x == 6), 'x'] <- 66 should be what you want.> I woul dlike to change the 6 in column x into 66 > why the above did not work?Best, Leon
Try this: data$x[data$x == 6] <- 66 On Tue, Aug 11, 2009 at 3:05 PM, SNN <s.nancy1@yahoo.com> wrote:> > Hi All, > > this could be a simple question but I am looking into modifying a data > frame > using a "condition" without the need to loop over that data, would that be > possible? > > I have tried the following > > > x<-c(4,5,6,6,8) > > y<-c("a","b","b","b","c") > > data<-data.frame(x,y) > > data > x y > 1 4 a > 2 5 b > 3 6 b > 4 6 b > 5 8 c > if (data$x==6){ > data$x<-66 > } > Warning message: > In if (data$x == 6) { : > the condition has length > 1 and only the first element will be used > > > > I woul dlike to change the 6 in column x into 66 > why the above did not work? > > Thanks > -- > View this message in context: > http://www.nabble.com/Problem-with-modifying-a-data-frame-tp24922814p24922814.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Dear Nancy, Try also: x <- c(4,5,6,6,8) y <- c("a","b","b","b","c") mydata <- data.frame(x,y) mydata mydata$x <- with(mydata, ifelse(x == 6, 66, x)) mydata See ?ifelse for more details. BTW, be careful: "data" is an R reserved name :-) HTH, Jorge On Tue, Aug 11, 2009 at 2:05 PM, SNN <> wrote:> > Hi All, > > this could be a simple question but I am looking into modifying a data > frame > using a "condition" without the need to loop over that data, would that be > possible? > > I have tried the following > > > x<-c(4,5,6,6,8) > > y<-c("a","b","b","b","c") > > data<-data.frame(x,y) > > data > x y > 1 4 a > 2 5 b > 3 6 b > 4 6 b > 5 8 c > if (data$x==6){ > data$x<-66 > } > Warning message: > In if (data$x == 6) { : > the condition has length > 1 and only the first element will be used > > > > I woul dlike to change the 6 in column x into 66 > why the above did not work? > > Thanks > -- > View this message in context: > http://www.nabble.com/Problem-with-modifying-a-data-frame-tp24922814p24922814.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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]]