I want to define a variable new_var by the following criteria:
For each id, if exist reference=1, then new_var=1; else new_var=0, for exemple:
id reference
A 0
A 1
B 0
B 0
Create a new_var
, the table looks like this:
id reference new_var
A 0 1
A 1 1
B 0 0
B 0 0
I only know that I can use the statement case when
in SAS, but in R how to do this?