samedi 9 mai 2020

Coercing different values to be equal based on another column's value

I know the title may be confusing and it may be hard for me to explain. I would like to take a data frame with repetitious values in the first column and change values in a different column to be the same for the repetitious values in the first column. So then I can call the unique() to reduce the table down.

For example:

rawdata<- data.frame(User=c('JSmith','JSmith','JDoe','JDoe','MDog','MDog','MDog') ,
                       Visit=c('Y','N','N','N','Y','N','Y'))
#displayed as  
   User    Visit
1 JSmith     Y
2 JSmith     N
3   JDoe     N
4   JDoe     N
5   MDog     Y
6   MDog     N
7   MDog     Y

#I would like to test the visit column for Y and if that is true for user's of the same name, 
#coerce that visit value to Y as well

 User    Visit
1 JSmith     Y
2 JSmith     Y
3   JDoe     N
4   JDoe     N
5   MDog     Y
6   MDog     Y
7   MDog     Y

#That way when I call unique(rawdata[,1]), it should output
 User    Visit
1 JSmith     Y
2   JDoe     N
3   MDog     Y

I used this simplified example for me to apply the principle to a much larger and more complicated data set, but the principle is the same. Unfortunately I don't know where to start. I was thinking a for loop with if statements but I'm not sure how to coerce the 2nd column values for all values in the first column that are equal. Any suggestions on how to tackle this. Thank you!

Aucun commentaire:

Enregistrer un commentaire