I have this df
df = data.frame(ID = rep(1:3,each=5),
group = c('A','B','A','A','C','B','D','C','B','A','A','B','B','A','C') )
and I wish to create dummy variables columns A,B,C and D with the result that if there is the value A, we make the value 1 eventhough there are many values A for the ID number 1.
The desired output is as follows
ID A B C D
1 1 1 1 1 0
2 2 1 1 1 1
3 3 1 1 1 0
+(as.data.frame.matrix(table(df)) > 0)
# A B C D
#1 1 1 1 0
#2 1 1 1 1
#3 1 1 1 0
Or with pivot_wider
:
tidyr::pivot_wider(df, names_from = "group", values_from = "group",
values_fn = ~ +(length(.x) > 0), values_fill = 0)