Amer
Amer

Reputation: 2251

Add FLAG based on certain condition in the data frame

 df <- 
 ID    AMT    
  1    NA     
  1    50    
  1    NA
  2    NA
  2    NA
  2    50


 dfout <- 
 ID    AMT   FLAG  
  1    NA     1
  1    50     0 
  1    NA     0
  2    NA     1 
  2    NA     1
  2    50     0

for each subject ID add a FLAG==1 when AMT is NA until you there is a row where AMT > 0, then the rest FLAG for that subject is 0. For example, for subject 1 in 'df' above FLAG==1 should be given for the first row for when AMT is NA. However, although last row is NA for the same subject, FLAG should be zero as we hit AMT>0 before it.

How to do this in R as I have a large dataset.

Upvotes: 0

Views: 76

Answers (1)

markdly
markdly

Reputation: 4534

This is one way it could be done:

library(tidyverse)

df <- tribble(
~ID,  ~AMT,    
1,    NA,     
1,    50,    
1,    NA,
2,    NA,
2,    NA,
2,    50)

df %>% 
  rownames_to_column() %>%
  group_by(ID) %>%
  mutate(x = as.numeric(rowname < min(rowname[!is.na(AMT)])))

#> # A tibble: 6 x 4
#> # Groups:   ID [2]
#>   rowname    ID   AMT     x
#>     <chr> <dbl> <dbl> <dbl>
#> 1       1     1    NA     1
#> 2       2     1    50     0
#> 3       3     1    NA     0
#> 4       4     2    NA     1
#> 5       5     2    NA     1
#> 6       6     2    50     0

Upvotes: 1

Related Questions