基本上,我想做this,但要使用分组变量。
例如,我有一个数据框,
data.frame(group = c(1, 1, 2, 2, 2), value = c("A", "B", "C", "D", "E", "F"))
我想按组将其转换为边缘列表:
from to group
A B 1
C D 2
D E 2
E F 2
请注意,没有B -> C
连接,因为它们位于单独的组中。
有人知道我会怎么做吗?
答案 0 :(得分:2)
library(tidyverse)
df%>%
group_by(group)%>%
mutate(from = value, to = lead(value), value = NULL)%>%
na.omit()
group from to
<dbl> <fct> <fct>
1 1 A B
2 2 C D
3 2 D E
4 2 E F
答案 1 :(得分:1)
使用data.table
library(data.table)
setDT(df1)[, .(from = value, to = shift(value, type = 'lead')),
by = .(group)][!is.na(to)]
#. group from to
#1: 1 A B
#2: 2 C D
#3: 2 D E
#4: 2 E F