Let's say I have the following df
df <- read.table(text = "team id result result_number
A 1 999 999
A 2 cat 45
A 3 dog 50
B 4 999 999
B 5 three 60
B 6 four 30
C 7 999 999
C 8 rabbit 45
C 9 monkey 11
D 10 dog 12
D 11 999 999
D 12 basket 10", header = T)
For every row that has a 999 in the result category, I'd like to replace that value (and the corresponding result_number value) with the values from a random row within the same team (e.g. random within A, B, C, or D) that is not 999.
So here's an example output:
df <- read.table(text = "team id result result_number
A 1 cat 45
A 2 cat 45
A 3 dog 50
B 4 four 30
B 5 three 60
B 6 four 30
C 7 rabbit 45
C 8 rabbit 45
C 9 monkey 11
D 10 dog 12
D 11 basket 10
D 12 basket 10", header = T)
I can't figure out an elegant way of doing this in dplyr without a really clumsy external function