问题
From my dataset I'm trying to make pairs based on a ranking. my data looks like
ID grp rank
1 grp1 1
1 grp2 1
1 grp3 2
2 grp1 1
2 grp2 2
2 grp2 2
2 grp2 2
2 grp3 2
2 grp1 3
The output I am aiming for is the following: for each ID
- if rank = 1 then grp in source and destination are the same = grp
- If rank is different from 1 then
- source = take grp from previous rank
- destination = take group from current rank If more then one group exist for the same ranking then an additional line needs to be created so that each pair is represented.
This looks then like the following
ID rank source destination
1 1 grp1 grp1
1 1 grp2 grp2
1 2 grp1 grp3
1 2 grp2 grp3
2 1 grp1 grp1
2 2 grp1 grp2
2 2 grp1 grp2
2 2 grp1 grp2
2 2 grp1 grp3
2 3 grp2 grp1
2 3 grp3 grp1
I started with a for loop and if_else statements but I got stuck. Any help is appreciated! thx in advance.
回答1:
We may do the following:
df %>% group_by(ID) %>%
do(map_dfr(1:nrow(.), function(i)
data.frame(.[i, -2], source = if(.$rank[i] == 1) .$grp[i] else unique(.$grp[.$rank == .$rank[i] - 1]),
destination = .$grp[i])))
# A tibble: 11 x 4
# Groups: ID [2]
# ID rank source destination
# <int> <int> <fct> <fct>
# 1 1 1 grp1 grp1
# 2 1 1 grp2 grp2
# 3 1 2 grp1 grp3
# 4 1 2 grp2 grp3
# 5 2 1 grp1 grp1
# 6 2 2 grp1 grp2
# 7 2 2 grp1 grp2
# 8 2 2 grp1 grp2
# 9 2 2 grp1 grp3
# 10 2 3 grp2 grp1
# 11 2 3 grp3 grp1
We group by ID
and then go over each row a given group. Then for each row we construct a new data frame according to your rules.
来源:https://stackoverflow.com/questions/53829063/creating-pairs-for-network-analysis