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

Daisy Englert Duursma daisy.duursma at gmail.com
Thu Jun 7 07:29:08 CEST 2012


Thanks, problem solved.

On Thu, Jun 7, 2012 at 1:58 PM, Daisy Englert Duursma
<daisy.duursma at gmail.com> 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
>
> #I can select the rows I want:
> dat2<-subset(dat1,x==1 & y==1)
> #replace the 1 with NA
> dat2$x<-rep(NA,nrow(dat2)
> dat2$y<-rep(NA,nrow(dat2)
>
> #select the other rows and rbind everything back together
> #This is where I get stuck
>
> #The end dataframe will look something like:
>
>   x y  fac
> NA NA   B
> NA NA   A
> 1 2   C
> 1 3   C
> 1 2   C
> 1 3   A
>
> #Is there a better way to do this where I do not need to subset
> perhaps using lapply?
>
>
> Thanks,
> Daisy
>
> --
> Daisy Englert Duursma
> Department of Biological Sciences
> Room E8C156
> Macquarie University, North Ryde, NSW 2109
> Australia
>
> Tel +61 2 9850 9256



-- 
Daisy Englert Duursma
Department of Biological Sciences
Room E8C156
Macquarie University, North Ryde, NSW 2109
Australia

Tel +61 2 9850 9256



More information about the R-help mailing list