Given this data frame (a simplified, essential reproducible example) A<-c(8,7,10,1,5) A_flag<-c(10,0,1,0,2) B<-c(5,6,2,1,0) B_flag<-c(12,9,0,5,0) mydf<-data.frame(A, A_flag, B, B_flag) # this is my initial df mydf I want to get to this final situation i<-which(mydf$A_flag==0) mydf$A[i]<-NA ii<-which(mydf$B_flag==0) mydf$B[ii]<-NA # this is my final df mydf By considering that I have to perform this task in a data frame with many columns I’m wondering if there is a compact and effective way to get the final result with just one ‘sweep’ of the dataframe? I was thinking to the function apply or lapply but I can not properly conceive how to… any hint for that? [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see 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. |
Hello,
Try the following. icol <- which(grepl("flag", names(mydf))) mydf[icol] <- lapply(mydf[icol], function(x){ is.na(x) <- x == 0 x }) mydf # A A_flag B B_flag #1 8 10 5 12 #2 7 NA 6 9 #3 10 1 2 NA #4 1 NA 1 5 #5 5 2 0 NA Hope this helps, Rui Barradas On 11/22/2017 10:34 AM, Massimo Bressan wrote: > > > Given this data frame (a simplified, essential reproducible example) > > > > > A<-c(8,7,10,1,5) > > A_flag<-c(10,0,1,0,2) > > B<-c(5,6,2,1,0) > > B_flag<-c(12,9,0,5,0) > > > > > mydf<-data.frame(A, A_flag, B, B_flag) > > > > > # this is my initial df > > mydf > > > > > I want to get to this final situation > > > > > i<-which(mydf$A_flag==0) > > mydf$A[i]<-NA > > > > > ii<-which(mydf$B_flag==0) > > mydf$B[ii]<-NA > > > > > # this is my final df > > mydf > > > > > By considering that I have to perform this task in a data frame with many columns I’m wondering if there is a compact and effective way to get the final result with just one ‘sweep’ of the dataframe? > > > > > I was thinking to the function apply or lapply but I can not properly conceive how to… > > > > > any hint for that? > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > 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. > ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see 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. |
...well, I don't think this is exactly the expected result (see my post)
to be noted that the columns affected should be "A" and "B" thanks for the help max ----- Messaggio originale ----- Da: "Rui Barradas" <[hidden email]> A: "Massimo Bressan" <[hidden email]>, "r-help" <[hidden email]> Inviato: Mercoledì, 22 novembre 2017 11:49:08 Oggetto: Re: [R] assign NA to rows by test on multiple columns of a data frame Hello, Try the following. icol <- which(grepl("flag", names(mydf))) mydf[icol] <- lapply(mydf[icol], function(x){ is.na(x) <- x == 0 x }) mydf # A A_flag B B_flag #1 8 10 5 12 #2 7 NA 6 9 #3 10 1 2 NA #4 1 NA 1 5 #5 5 2 0 NA Hope this helps, Rui Barradas On 11/22/2017 10:34 AM, Massimo Bressan wrote: > > > Given this data frame (a simplified, essential reproducible example) > > > > > A<-c(8,7,10,1,5) > > A_flag<-c(10,0,1,0,2) > > B<-c(5,6,2,1,0) > > B_flag<-c(12,9,0,5,0) > > > > > mydf<-data.frame(A, A_flag, B, B_flag) > > > > > # this is my initial df > > mydf > > > > > I want to get to this final situation > > > > > i<-which(mydf$A_flag==0) > > mydf$A[i]<-NA > > > > > ii<-which(mydf$B_flag==0) > > mydf$B[ii]<-NA > > > > > # this is my final df > > mydf > > > > > By considering that I have to perform this task in a data frame with many columns I’m wondering if there is a compact and effective way to get the final result with just one ‘sweep’ of the dataframe? > > > > > I was thinking to the function apply or lapply but I can not properly conceive how to… > > > > > any hint for that? > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > 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. > ------------------------------------------------------------ Massimo Bressan ARPAV Agenzia Regionale per la Prevenzione e Protezione Ambientale del Veneto Dipartimento Provinciale di Treviso Via Santa Barbara, 5/a 31100 Treviso, Italy tel: +39 0422 558545 fax: +39 0422 558516 e-mail: [hidden email] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see 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. |
Hello,
Sorry, I obviously read in a hurry. icol <- grepl("flag", names(mydf)) is.na(mydf[!icol]) <- mydf[icol] == 0 mydf # A A_flag B B_flag #1 8 10 5 12 #2 NA 0 6 9 #3 10 1 NA 0 #4 NA 0 1 5 #5 5 2 NA 0 Hope this helps, Rui Barradas On 11/22/2017 11:19 AM, Massimo Bressan wrote: > ...well, I don't think this is exactly the expected result (see my post) > > to be noted that the columns affected should be "A" and "B" > > thanks for the help > > max > > ----- Messaggio originale ----- > Da: "Rui Barradas" <[hidden email]> > A: "Massimo Bressan" <[hidden email]>, "r-help" <[hidden email]> > Inviato: Mercoledì, 22 novembre 2017 11:49:08 > Oggetto: Re: [R] assign NA to rows by test on multiple columns of a data frame > > Hello, > > Try the following. > > > icol <- which(grepl("flag", names(mydf))) > mydf[icol] <- lapply(mydf[icol], function(x){ > is.na(x) <- x == 0 > x > }) > > mydf > # A A_flag B B_flag > #1 8 10 5 12 > #2 7 NA 6 9 > #3 10 1 2 NA > #4 1 NA 1 5 > #5 5 2 0 NA > > > Hope this helps, > > Rui Barradas > > On 11/22/2017 10:34 AM, Massimo Bressan wrote: >> >> >> Given this data frame (a simplified, essential reproducible example) >> >> >> >> >> A<-c(8,7,10,1,5) >> >> A_flag<-c(10,0,1,0,2) >> >> B<-c(5,6,2,1,0) >> >> B_flag<-c(12,9,0,5,0) >> >> >> >> >> mydf<-data.frame(A, A_flag, B, B_flag) >> >> >> >> >> # this is my initial df >> >> mydf >> >> >> >> >> I want to get to this final situation >> >> >> >> >> i<-which(mydf$A_flag==0) >> >> mydf$A[i]<-NA >> >> >> >> >> ii<-which(mydf$B_flag==0) >> >> mydf$B[ii]<-NA >> >> >> >> >> # this is my final df >> >> mydf >> >> >> >> >> By considering that I have to perform this task in a data frame with many columns I’m wondering if there is a compact and effective way to get the final result with just one ‘sweep’ of the dataframe? >> >> >> >> >> I was thinking to the function apply or lapply but I can not properly conceive how to… >> >> >> >> >> any hint for that? >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> [hidden email] mailing list -- To UNSUBSCRIBE and more, see >> 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. >> ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see 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. |
In reply to this post by Massimo Bressan
Hi *Massimo,*
*Try this.* *a <- mydf==0mydf[a] <- NAHTHEK* On Wed, Nov 22, 2017 at 5:34 AM, Massimo Bressan < [hidden email]> wrote: > > > Given this data frame (a simplified, essential reproducible example) > > > > > A<-c(8,7,10,1,5) > > A_flag<-c(10,0,1,0,2) > > B<-c(5,6,2,1,0) > > B_flag<-c(12,9,0,5,0) > > > > > mydf<-data.frame(A, A_flag, B, B_flag) > > > > > # this is my initial df > > mydf > > > > > I want to get to this final situation > > > > > i<-which(mydf$A_flag==0) > > mydf$A[i]<-NA > > > > > ii<-which(mydf$B_flag==0) > > mydf$B[ii]<-NA > > > > > # this is my final df > > mydf > > > > > By considering that I have to perform this task in a data frame with many > columns I’m wondering if there is a compact and effective way to get the > final result with just one ‘sweep’ of the dataframe? > > > > > I was thinking to the function apply or lapply but I can not properly > conceive how to… > > > > > any hint for that? > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > 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]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see 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. |
OPS,
Sorry i did not read the post carfully. Mine will not work if you have zeros on columns A and B.. But you could modify it to work for specific columns i believe. EK On Wed, Nov 22, 2017 at 8:37 AM, Ek Esawi <[hidden email]> wrote: > Hi *Massimo,* > > *Try this.* > > *a <- mydf==0mydf[a] <- NAHTHEK* > > On Wed, Nov 22, 2017 at 5:34 AM, Massimo Bressan < > [hidden email]> wrote: > >> >> >> Given this data frame (a simplified, essential reproducible example) >> >> >> >> >> A<-c(8,7,10,1,5) >> >> A_flag<-c(10,0,1,0,2) >> >> B<-c(5,6,2,1,0) >> >> B_flag<-c(12,9,0,5,0) >> >> >> >> >> mydf<-data.frame(A, A_flag, B, B_flag) >> >> >> >> >> # this is my initial df >> >> mydf >> >> >> >> >> I want to get to this final situation >> >> >> >> >> i<-which(mydf$A_flag==0) >> >> mydf$A[i]<-NA >> >> >> >> >> ii<-which(mydf$B_flag==0) >> >> mydf$B[ii]<-NA >> >> >> >> >> # this is my final df >> >> mydf >> >> >> >> >> By considering that I have to perform this task in a data frame with many >> columns I’m wondering if there is a compact and effective way to get the >> final result with just one ‘sweep’ of the dataframe? >> >> >> >> >> I was thinking to the function apply or lapply but I can not properly >> conceive how to… >> >> >> >> >> any hint for that? >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> [hidden email] mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posti >> ng-guide.html >> and provide commented, minimal, self-contained, reproducible code. > > > [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see 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. |
Hi--
I too misread the question twice and i may have mistakenly posted non-text answer earlier. Below is a step by step solution that works provided that your real data frame has the same structure (alternative columns as in your example). You could combine all the steps in 2 statements. Best of luck EK a <- grep("_flag",colnames(mydf)) b <- mydf[,a]==0 c <- mydf[,a-1] c[b] <- NA mydf[,a-1] <- c mydf A A_flag B B_flag 1 8 10 5 12 2 NA 0 6 9 3 10 1 NA 0 4 NA 0 1 5 5 5 2 NA 0 On Wed, Nov 22, 2017 at 8:44 AM, Ek Esawi <[hidden email]> wrote: > OPS, > > Sorry i did not read the post carfully. Mine will not work if you have > zeros on columns A and B.. But you could modify it to work for specific > columns i believe. > > EK > > On Wed, Nov 22, 2017 at 8:37 AM, Ek Esawi <[hidden email]> wrote: > >> Hi *Massimo,* >> >> *Try this.* >> >> *a <- mydf==0mydf[a] <- NAHTHEK* >> >> On Wed, Nov 22, 2017 at 5:34 AM, Massimo Bressan < >> [hidden email]> wrote: >> >>> >>> >>> Given this data frame (a simplified, essential reproducible example) >>> >>> >>> >>> >>> A<-c(8,7,10,1,5) >>> >>> A_flag<-c(10,0,1,0,2) >>> >>> B<-c(5,6,2,1,0) >>> >>> B_flag<-c(12,9,0,5,0) >>> >>> >>> >>> >>> mydf<-data.frame(A, A_flag, B, B_flag) >>> >>> >>> >>> >>> # this is my initial df >>> >>> mydf >>> >>> >>> >>> >>> I want to get to this final situation >>> >>> >>> >>> >>> i<-which(mydf$A_flag==0) >>> >>> mydf$A[i]<-NA >>> >>> >>> >>> >>> ii<-which(mydf$B_flag==0) >>> >>> mydf$B[ii]<-NA >>> >>> >>> >>> >>> # this is my final df >>> >>> mydf >>> >>> >>> >>> >>> By considering that I have to perform this task in a data frame with >>> many columns I’m wondering if there is a compact and effective way to get >>> the final result with just one ‘sweep’ of the dataframe? >>> >>> >>> >>> >>> I was thinking to the function apply or lapply but I can not properly >>> conceive how to… >>> >>> >>> >>> >>> any hint for that? >>> >>> [[alternative HTML version deleted]] >>> >>> ______________________________________________ >>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see >>> https://stat.ethz.ch/mailman/listinfo/r-help >>> PLEASE do read the posting guide http://www.R-project.org/posti >>> ng-guide.html >>> and provide commented, minimal, self-contained, reproducible code. >> >> >> > [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see 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. |
In reply to this post by Massimo Bressan
Do you mean like this:
mydf <- within(mydf, { is.na(A)<- !A_flag is.na(B)<- !B_flag } ) > mydf A A_flag B B_flag 1 8 10 5 12 2 NA 0 6 9 3 10 1 NA 0 4 NA 0 1 5 5 5 2 NA 0 Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Wed, Nov 22, 2017 at 2:34 AM, Massimo Bressan < [hidden email]> wrote: > > > Given this data frame (a simplified, essential reproducible example) > > > > > A<-c(8,7,10,1,5) > > A_flag<-c(10,0,1,0,2) > > B<-c(5,6,2,1,0) > > B_flag<-c(12,9,0,5,0) > > > > > mydf<-data.frame(A, A_flag, B, B_flag) > > > > > # this is my initial df > > mydf > > > > > I want to get to this final situation > > > > > i<-which(mydf$A_flag==0) > > mydf$A[i]<-NA > > > > > ii<-which(mydf$B_flag==0) > > mydf$B[ii]<-NA > > > > > # this is my final df > > mydf > > > > > By considering that I have to perform this task in a data frame with many > columns I’m wondering if there is a compact and effective way to get the > final result with just one ‘sweep’ of the dataframe? > > > > > I was thinking to the function apply or lapply but I can not properly > conceive how to… > > > > > any hint for that? > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > 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]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see 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. |
yes, it works, even if I do not really get how and why it's working the combination of logical results (could you provide some insights for that?)
moreover, and most of all, I was hoping for a compact solution because I need to deal with MANY columns (more than 40) in data frame with the same basic structure as the simplified example I posted thanks m ----- Messaggio originale ----- Da: "Bert Gunter" <[hidden email]> A: "Massimo Bressan" <[hidden email]> Cc: "r-help" <[hidden email]> Inviato: Mercoledì, 22 novembre 2017 17:32:33 Oggetto: Re: [R] assign NA to rows by test on multiple columns of a data frame Do you mean like this: mydf <- within(mydf, { is.na(A)<- !A_flag is.na(B)<- !B_flag } ) > mydf A A_flag B B_flag 1 8 10 5 12 2 NA 0 6 9 3 10 1 NA 0 4 NA 0 1 5 5 5 2 NA 0 Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Wed, Nov 22, 2017 at 2:34 AM, Massimo Bressan < [hidden email]> wrote: > > > Given this data frame (a simplified, essential reproducible example) > > > > > A<-c(8,7,10,1,5) > > A_flag<-c(10,0,1,0,2) > > B<-c(5,6,2,1,0) > > B_flag<-c(12,9,0,5,0) > > > > > mydf<-data.frame(A, A_flag, B, B_flag) > > > > > # this is my initial df > > mydf > > > > > I want to get to this final situation > > > > > i<-which(mydf$A_flag==0) > > mydf$A[i]<-NA > > > > > ii<-which(mydf$B_flag==0) > > mydf$B[ii]<-NA > > > > > # this is my final df > > mydf > > > > > By considering that I have to perform this task in a data frame with many > columns I’m wondering if there is a compact and effective way to get the > final result with just one ‘sweep’ of the dataframe? > > > > > I was thinking to the function apply or lapply but I can not properly > conceive how to… > > > > > any hint for that? > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > 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. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see 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. |
Inline.
-- Bert On Wed, Nov 22, 2017 at 11:52 PM, Massimo Bressan < [hidden email]> wrote: > yes, it works, even if I do not really get how and why it's working the > combination of logical results (could you provide some insights for that?) > Read the docs! ?"!" ?NA explain Then a more "compact" (but probably less efficient) solution just loops over the indices: ##UNTESTED choices <- ***column numbers of _flag columns*** chng <- ***column numbers of columns that will have values changed to NA*** for(i in seq_along choices) mydf[, ] <- is.na(mydf[, chng[i]) <- !mydf[, choices[i]] > > moreover, and most of all, I was hoping for a compact solution because I > need to deal with MANY columns (more than 40) in data frame with the same > basic structure as the simplified example I posted > > thanks > > m > > > ----- Messaggio originale ----- > Da: "Bert Gunter" <[hidden email]> > A: "Massimo Bressan" <[hidden email]> > Cc: "r-help" <[hidden email]> > Inviato: Mercoledì, 22 novembre 2017 17:32:33 > Oggetto: Re: [R] assign NA to rows by test on multiple columns of a data > frame > > Do you mean like this: > > mydf <- within(mydf, { > is.na(A)<- !A_flag > is.na(B)<- !B_flag > } > ) > > > mydf > A A_flag B B_flag > 1 8 10 5 12 > 2 NA 0 6 9 > 3 10 1 NA 0 > 4 NA 0 1 5 > 5 5 2 NA 0 > > > Cheers, > Bert > > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along and > sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > On Wed, Nov 22, 2017 at 2:34 AM, Massimo Bressan < > [hidden email]> wrote: > > > > > > > Given this data frame (a simplified, essential reproducible example) > > > > > > > > > > A<-c(8,7,10,1,5) > > > > A_flag<-c(10,0,1,0,2) > > > > B<-c(5,6,2,1,0) > > > > B_flag<-c(12,9,0,5,0) > > > > > > > > > > mydf<-data.frame(A, A_flag, B, B_flag) > > > > > > > > > > # this is my initial df > > > > mydf > > > > > > > > > > I want to get to this final situation > > > > > > > > > > i<-which(mydf$A_flag==0) > > > > mydf$A[i]<-NA > > > > > > > > > > ii<-which(mydf$B_flag==0) > > > > mydf$B[ii]<-NA > > > > > > > > > > # this is my final df > > > > mydf > > > > > > > > > > By considering that I have to perform this task in a data frame with many > > columns I’m wondering if there is a compact and effective way to get the > > final result with just one ‘sweep’ of the dataframe? > > > > > > > > > > I was thinking to the function apply or lapply but I can not properly > > conceive how to… > > > > > > > > > > any hint for that? > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > > 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]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see 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. |
Free forum by Nabble | Edit this page |