将纵向数据转换为边列表和节点列表

Convert longitudinal data to edgelist and nodelist

我有一些纵向数据,我想将其转换为有向边列表和节点列表。 edgelist 有一个权重列,显示 let 的计数从一天到下一天的变化。以下是纵向数据:

longdata <- data.frame(    id = c(1L,1L,1L,1L,1L,2L,2L,2L,
                                  2L,2L,3L,3L,3L,3L,3L),
                           day = c(1L,2L,3L,4L,5L,1L,2L,3L,
                                   4L,5L,1L,2L,3L,4L,5L),
                           let = c("o","s","s","o","s","a",
                                   "b","a","b","a","c","c","d","d","c"),
                           gender = c("F","F","F","F","F","M",
                                      "M","M","M","M", "F","F","F","F","F"),
                           age = c(23L,23L,23L,23L,23L,31L,
                                   31L,31L,31L,31L,28L,28L,28L,28L,28L)
)

这里是我要获取的边缘列表:

from to weight
  o  o      0
  o  s      2
  s  o      1
  s  s      1
  a  a      0
  a  b      2
  b  a      2
  b  b      0
  c  c      1
  c  d      1
  d  c      1
  d  d      1

这里是我要获取的节点列表:

id gender age
   1      F  23
   2      M  31
   3      F  28
longdata %>%
   group_by(id) %>%
   transmute(from = factor(let, unique(let)), to = lead(from)) %>%
   count(from, to) %>%
   na.omit()

# A tibble: 9 x 4
# Groups:   id [3]
     id from  to        n
  <int> <fct> <fct> <int>
1     1 o     s         2
2     1 s     o         1
3     1 s     s         1
4     2 a     b         2
5     2 b     a         2
6     3 c     c         1
7     3 c     d         1
8     3 d     c         1
9     3 d     d         1

distinct(longdata, id, gender, age)
  id gender age
1  1      F  23
2  2      M  31
3  3      F  28