Missing flag for the same records

Hi,
I have a question. I have this simple df:

data.source <- data.frame(
  stringsAsFactors = FALSE,
             RegNo = c("0062JUD","1001WW","1001WW",
                       "1001WW","100CM","1010DD","1010DD","1010DD","1010DD",
                       "99HMT","99HMT","99HMT","AD15ODA"),
      Retained.Reg = c(1, 1, NA, NA, NA, NA, NA, 1, 1, NA, 1, 1, NA)
)

As you can see the flag (Retained.Reg) is not consistent for all records (RegNo).
How can I copy that with a rule: if at least one RegNo is flagged, all same RegNo should be flagged?

I know I could possibly add a column calculating average for each RegNo but how?

There are many ways to do this, here is one

library(tidyverse)

data.source <- data.frame(
    stringsAsFactors = FALSE,
    RegNo = c("0062JUD","1001WW","1001WW",
              "1001WW","100CM","1010DD","1010DD","1010DD","1010DD",
              "99HMT","99HMT","99HMT","AD15ODA"),
    Retained.Reg = c(1, 1, NA, NA, NA, NA, NA, 1, 1, NA, 1, 1, NA)
)

data.source %>%
    group_by(RegNo) %>% 
    mutate(Retained.Reg = as.numeric(any(as.logical(Retained.Reg))))
#> # A tibble: 13 × 2
#> # Groups:   RegNo [6]
#>    RegNo   Retained.Reg
#>    <chr>          <dbl>
#>  1 0062JUD            1
#>  2 1001WW             1
#>  3 1001WW             1
#>  4 1001WW             1
#>  5 100CM             NA
#>  6 1010DD             1
#>  7 1010DD             1
#>  8 1010DD             1
#>  9 1010DD             1
#> 10 99HMT              1
#> 11 99HMT              1
#> 12 99HMT              1
#> 13 AD15ODA           NA

Created on 2023-03-04 with reprex v2.0.2

This would be a hacky way to do it and not conceptually sound so I don't recommend it.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.