Home > OS >  Create variable that flags an ID if it has existed in any previous month
Create variable that flags an ID if it has existed in any previous month

Time:06-11

I am unsure of how to create a variable that flags an ID in the current month if the ID has existed in any previous month.

Example data:

ID<-c(1,2,3,2,3,4,1,5)
Month<-c(1,1,1,2,2,2,3,3)
Flag<-c(0,0,0,1,1,0,1,0)

have<-cbind(ID,Month)

> have
ID  Month
1     1
2     1
3     1
2     2
3     2
4     2
1     3
5     3

want:

> want
ID  Month Flag
1     1    0
2     1    0
3     1    0
2     2    1
3     2    1
4     2    0
1     3    1
5     3    0

CodePudding user response:

a data.table approach

library(data.table)
# set to data.table format
DT <- as.data.table(have)
# initialise Signal column
DT[, Signal := 0]
# flag duplicates with a 1
DT[duplicated(ID), Signal := 1, by = Month][]


   ID Month Signal
1:  1     1      0
2:  2     1      0
3:  3     1      0
4:  2     2      1
5:  3     2      1
6:  4     2      0
7:  1     3      1
8:  5     3      0

CodePudding user response:

The idea is presented from akrun in the comments. Here is the dplyr application:

First use as_tibble to bring matrix in tibble format then use an ifelse statement with duplicated as @akrun already suggests.

library(tibble)
library(dplyr)

have %>% 
  as_tibble() %>% 
  mutate(flag = ifelse(duplicated(ID),1,0))
     ID Month  flag
  <dbl> <dbl> <dbl>
1     1     1     0
2     2     1     0
3     3     1     0
4     2     2     1
5     3     2     1
6     4     2     0
7     1     3     1
8     5     3     0
  •  Tags:  
  • r
  • Related