lundi 25 juin 2018

Conditional Formatting of grouped Data in R

UPRN    Start.Date  End.Date  Disability
1       2006-12-20 17-NOV-17         Y
1       2006-12-20 17-NOV-17         N
2       1991-12-06                   N
2       1991-12-06                   N
3       1991-04-29 2015-04-21        N
3       2015-04-22                   Y
4       2005-02-15                   Y
4       2005-02-15                   N

I have a dataset that looks something like the above (but much bigger). I would like to create a new column called Any_Disability.

The way I want to do this is to group by UPRN, Start.Date and End.Date and if any row in that group has a disability then both rows would have a "Y" for Any_Disability.

What I've tried already is:

test3<-all_data%>%
  group_by(UPRN, Start.Date, End.Date)%>%
  mutate(Any_Disability = ifelse(Disability=="Y", "Y","N"))

But this doesn't work as it gives the below answer:

UPRN    Start.Date  End.Date  Disability  Any_Disability
1       2006-12-20 17-NOV-17         Y          Y
1       2006-12-20 17-NOV-17         N          N
2       1991-12-06                   N          N
2       1991-12-06                   N          N
3       1991-04-29 2015-04-21        N          N
3       2015-04-22                   Y          Y
4       2005-02-15                   Y          Y
4       2005-02-15                   N          N

Aucun commentaire:

Enregistrer un commentaire