mercredi 15 août 2018

Apply value to ID's that are the same R

Using the below dataset as an example:

  id flag
1  1    Y
2  1     
3  1     
4  2     
5  2    Y
6  3    Y

Reproducible example:

df <- data.frame(id=factor(c(1,1,1,2,2,3)), flag=c('Y','','','','Y',''))

If there exists a flag = Y for any ID then I want all that ID to have the flag "Y" so the above example would look as follows:

  id flag
1  1    Y
2  1    Y
3  1    Y
4  2    Y
5  2    Y
6  3    Y

Aucun commentaire:

Enregistrer un commentaire