I have a large data table that has a similar structure to this reproducible example:
d <- data.table(
g =c(1 ,1 ,2 ,2 ,2 ,3 ,3 , 4 , 4),
A =c('a','a','b','b','w','c','q','r','r'),
B =c(1 ,1 ,2 ,2 ,2 ,3 ,3 ,4 , 5),
sameA =c(T , T , F , F , F , F , F , T , T),
sameAB=c(T , T , F , F , F , F , F , F , F)
)
g
indicates the group and A
and B
are individual characteristics. I want to create the variables:
sameA
: indicates that A is the same for all observations of the groupsameAB
: indicates that A and B are the same for all observations of the group
how can I do this in an efficient way using data.table?