[R] assign NA to rows by test on multiple columns of a data frame

Bert Gunter bgunter.4567 at gmail.com
Wed Nov 22 17:32:33 CET 2017


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 <
massimo.bressan at arpa.veneto.it> 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]]
>
> ______________________________________________
> R-help at r-project.org 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]]



More information about the R-help mailing list