Creating an indicator variable in panel data in R
I feel like it should be easy, but at a loss, and I hope you can help. I have a panel data, id
with variables, here it is just v1
:
id v1
A 14
A 15
B 12
B 13
B 14
C 11
C 12
C 13
D 14
I would just like to create a dummy variable to indicate if a value v1
(say 12
) exists in the panel for id
. So something like:
id v1 v2
A 14 0
A 15 0
B 12 1
B 13 1
B 14 1
C 11 1
C 12 1
C 13 1
D 14 0
I believe this should be simple, but cannot find an easy one-line solution.
Many thanks!
source to share
Try
library(dplyr)
df %>% group_by(id) %>% mutate(v2 = as.numeric(any(v1 == 12)))
Or as suggested by @akrun:
library(data.table)
setDT(df)[, v2 := any(v1 ==12)+0L, id]
Note. Adding 0L
to booleans created any()
will switch TRUE/FALSE
to 0
and 1
s.
Another approach could be using ave
:
df$v2 <- with(df, ave(v1, id, FUN = function(x) any(x == 12)))
What gives:
# id v1 v2
#1 A 14 0
#2 A 15 0
#3 B 12 1
#4 B 13 1
#5 B 14 1
#6 C 11 1
#7 C 12 1
#8 C 13 1
#9 D 14 0
source to share