My dataframe DF
looks like:
ID Name1 Name2 Group
1234 A1 x
1234 A4 w
1234 A3 q
1234 A A
1234 A2 z
5678 B3 s
5678 B B
...
I need to add a column for Group
that is Name1
for each ID
that matches the ID
in the row where Name1 == Name2
.
So the logic would be to check if Name1 == Name2, remember the ID of that Row and the Name1 value, then for every row having that ID, put the Name1 Value in each row of the Group column.
The result should look like:
ID Name1 Name2 Group
1234 A1 x A
1234 A4 w A
1234 A3 q A
1234 A A A
1234 A2 z A
5678 B3 s B
5678 B B B
...
I am not sure how to do this in the dataframe though and from Many rows with different IDs. I dont want to use loops.
mutate()
or lapply()
maybe?
I can see how to add the Name1 value in the Group column for the rows where Name1==Name2, but how do I roll that back up for all matching IDs?