Re: [R] conditional statement to replace values in dataframe with NA

2012-06-07 Thread arun
Hi, Try this,> dat1 <- data.frame(x=rep(1,6),y=rep(1:3,2), fac=sample(L3, 6, replace=TRUE)) > dat1   x y fac 1 1 1   C 2 1 2   B 3 1 3   B 4 1 1   A 5 1 2   B 6 1 3   B > dat1[dat1$x==1&dat1$y==1,1:2]<-NA > dat1    x  y fac 1 NA NA   C 2  1  2   B 3  1  3   B 4 NA NA   A 5  1  2   B 6  1  3   B

Re: [R] conditional statement to replace values in dataframe with NA

2012-06-07 Thread peter dalgaard
On Jun 7, 2012, at 07:28 , Bert Gunter wrote: > Actually, recycling makes the rep(NA,2) business unnecessary. Simply: > > dat1[dat1$x==1 & dat1$y==1,1:2] <- rep(NA,2) > > ##or > > with(dat1,{dat1[x==1 & y==1,1:2] <- NA;dat1}) > > will do it. > Or, use the assignment form of is.na: cond <-

Re: [R] conditional statement to replace values in dataframe with NA

2012-06-06 Thread Daisy Englert Duursma
Thanks, problem solved. On Thu, Jun 7, 2012 at 1:58 PM, Daisy Englert Duursma wrote: > Hello and thanks for helping. > > #some data > L3 <- LETTERS[1:3] > dat1 <- data.frame(cbind(x=1, y=rep(1:3,2), fac=sample(L3, 6, replace=TRUE))) > > > #When x==1 and y==1 I want to replace the 1 values with NA

Re: [R] conditional statement to replace values in dataframe with NA

2012-06-06 Thread Bert Gunter
Actually, recycling makes the rep(NA,2) business unnecessary. Simply: dat1[dat1$x==1 & dat1$y==1,1:2] <- rep(NA,2) ##or with(dat1,{dat1[x==1 & y==1,1:2] <- NA;dat1}) will do it. -- Bert On Wed, Jun 6, 2012 at 10:21 PM, Bert Gunter wrote: > Have you read "An Intro to R?" If not,please do so

Re: [R] conditional statement to replace values in dataframe with NA

2012-06-06 Thread Bert Gunter
Have you read "An Intro to R?" If not,please do so before posting further. The way you are going about things makes me think you haven't, but ... This **is** a slightly tricky application of indexing, if I understand you correctly. Here are two essentially identical ways to do it, but the second i