I have information on physicians working in different hospitals at different points in time. I would like to output a dataframe with that informs each pair of physician in each hospital. I would like to see each pair only once in the dataset; meaning that if physicians A and B work together in the same hospital I would like to see either the pair A-B or the pair B-A, but not both.
Consider the very simple example of hospitals x-y-w, periods 1-2 and physicians A-B-C-D.
mydf <- data.frame(hospital = c("x","x","x","x","x","y","y","y","w","w","w","w"),
period = c(1,1,1,2,2,1,2,2,1,1,2,2),
physician = c("A","B","C","A","B","A","A","C","C","D","A","D"))
Below I manage to get all pairs, however each pair shows twice (swapping between from and to). How could get each pair only showing up once in the output?
pairs_df <- mydf %>%
rename(from = physician) %>%
left_join(mydf, by=c("hospital","period")) %>%
rename(to = physician) %>%
filter(from!=to)