Daisy Englert Duursma
2012-Jun-07 03:58 UTC
[R] conditional statement to replace values in dataframe with NA
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
arun
2012-Jun-07 05:02 UTC
[R] conditional statement to replace values in dataframe with NA
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 A.K. ----- Original Message ----- From: Daisy Englert Duursma <daisy.duursma at gmail.com> To: "r-help at R-project.org" <r-help at r-project.org> Cc: Sent: Wednesday, June 6, 2012 11:58 PM Subject: [R] conditional statement to replace values in dataframe with NA 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 ______________________________________________ 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.
Bert Gunter
2012-Jun-07 05:21 UTC
[R] conditional statement to replace values in dataframe with NA
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 is a little trickier ## First> dat1[dat1$x==1 & dat1$y==1,1:2] <- rep(NA,2) > dat1x y fac 1 <NA> <NA> A 2 1 2 B 3 1 3 A 4 <NA> <NA> C 5 1 2 A 6 1 3 C ##Slightly trickier version using with() to avoid explicit extraction from data frame ## Reconstitute dat1> dat1x y fac 1 1 1 C 2 1 2 C 3 1 3 B 4 1 1 B 5 1 2 C 6 1 3 B dat1 <- with(dat1,{dat1[x==1 & y==1,1:2] <- rep(NA,2); dat1})> dat1x y fac 1 <NA> <NA> B 2 1 2 A 3 1 3 A 4 <NA> <NA> C 5 1 2 A 6 1 3 B ## ?with for explanation -- Bert On Wed, Jun 6, 2012 at 8: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 > > ______________________________________________ > 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
Daisy Englert Duursma
2012-Jun-07 05:29 UTC
[R] conditional statement to replace values in dataframe with NA
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