I'm trying to do a social network analysis and create a data frame of associations. I have 2 columns, one with ID and one with Date. I want to separate the ID column into a "from" and "to" column based on day to see on which days two ID's were seen together.
This is what my data looks like:
Date ID
1 2010-01-02 A
2 2010-01-02 A
3 2010-01-02 A
4 2010-01-02 A
5 2010-01-02 A
6 2010-01-02 A
7 2010-01-02 A
8 2010-01-02 A
9 2010-01-02 A
10 2010-01-02 W
11 2010-01-02 W
12 2010-01-02 W
13 2010-01-02 W
14 2010-01-11 A
15 2010-01-11 A
16 2010-01-11 A
17 2010-01-11 A
18 2010-01-11 A
19 2010-01-11 A
20 2010-01-11 A
21 2010-01-11 A
22 2010-01-11 A
23 2010-01-11 G
24 2010-01-11 G
25 2010-01-11 G
26 2010-01-11 K
27 2010-01-11 K
28 2010-01-11 K
29 2010-01-11 W
30 2010-01-11 W
and I want it to look like this:
Date From To
2010-01-02 A W
2010-01-11 A G
2010-01-11 A K
2010-01-11 A W
2010-01-11 W G
2010-01-11 W K
2010-01-11 G K
I don't know that there's an automatic function in
dplyror related packages, but we can do this:Steps:
combn(unique(ID), 2)finds the combinations for the observedIDs.by="Date"is one ofdplyr's ways to do grouped ops "once"combnreturns a matrix, we need to transpose it and frame it, withdata.frame(t(...)); since the default names are"V1"and"V2", we wrap that insetNamesto get the names you wantdplyr's data verbs (e.g.,mutate,summarize,reframe) is that if the inner expression returns a frame, it will be appended (cbind-style) to the existing frame formutate, and replace it (with groups) withsummarize/reframe, names and all. (This is one reason why I wrapped indata.frame(.).)Data