长度格式的组合
因此,我有一个父母及其子女的数据集以下形式,
Children_id Parent_id
10 1
11 1
12 1
13 2
14 2
我想要的是每个孩子的兄弟姐妹长期的数据集,即,
id sibling_id
10 11
10 12
11 10
11 12
12 10
12 11
13 14
14 13
最好使用DataTable,最好的方法是什么?
示例数据:
df< - data.frame(“ children_id” = c(10,11,12,13,14),“ parent_id” = c(1,1,1, 2,2))
So I have a dataset of parents and their children of the following form
Children_id Parent_id
10 1
11 1
12 1
13 2
14 2
What I want is a dataset of each child's siblings in long format, i.e.,
id sibling_id
10 11
10 12
11 10
11 12
12 10
12 11
13 14
14 13
What's the best way to achieve this, preferably using datatable?
Example data:
df <- data.frame("Children_id" = c(10,11,12,13,14), "Parent_id" = c(1,1,1,2,2))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
外面的图表专家可能会有更好的解决方案,但这是
data.table
解决方案:输出:输出:
The graph experts out there will probably have better solutions, but here is a
data.table
solution:Output:
在
基础r
中,我们可以在explive.grid.grid
之后split
ting-output
或使用
data.table.table
代码> CJIn
base R
, we can useexpand.grid
aftersplit
ting-output
Or using
data.table
withCJ
dplyr
带有innion_join
的解决方案:或其他策略:
A
dplyr
solution withinner_join
:or another strategy: