r重塑高到宽,有许多关系表
我有一张桌子和父母及其孩子的信息(名称,年级,年龄)。一个孩子可以有多个父母,一个父母可以有多个孩子。我想重塑桌子,以便每行都是一个独特的父母,所有他或她的孩子的信息都标有.1,2等。
原始表:
parentid | childn childn | childgrade | childgrade childing | heookerid |
---|---|---|---|---|
1 | x | 10 | 21 | a |
2 | x | 10 | 21 | a |
3 | z | 12 | 23 | b |
1 | y | 13 | 24 | A |
2 | y | 13 | 24 | A |
3 | T | 15 | 26 | B |
4 | G | 16 | 27 | C |
目标:
parentid | childn.1 | Childgrade.1 | Childage.1 Childn.2 | Childn.2 | Childgrade.2 | Childge.2 ersevelage.2 | ereppordid |
---|---|---|---|---|---|---|---|
1 | x | 10 | 21 | y | 13 | 24 | a |
2 | x | 10 | 21 | y | 13 | 24 | a |
3 | z | 12 | 23 | t | 15 | 26 | b |
4 | g | 16 | 27 | na na | n na n | n na n | c |
我想知道如何在R中实现这一目标。非常感谢。
I have a table with parent and his or her child's info(name, grade, age). One child can have multiple parents and one parents can have multiple children. I want to reshape the table so each row will be an unique parent with all his or her children's information labeled as .1, ,2 etc.
Original Table:
ParentID | ChildN | ChildGrade | ChildAge | HouseholdID |
---|---|---|---|---|
1 | x | 10 | 21 | a |
2 | x | 10 | 21 | a |
3 | z | 12 | 23 | b |
1 | y | 13 | 24 | a |
2 | y | 13 | 24 | a |
3 | t | 15 | 26 | b |
4 | g | 16 | 27 | c |
Goal:
ParentID | ChildN.1 | ChildGrade.1 | ChildAge.1 | ChildN.2 | ChildGrade.2 | ChildAge.2 | HouseholdID |
---|---|---|---|---|---|---|---|
1 | x | 10 | 21 | y | 13 | 24 | a |
2 | x | 10 | 21 | y | 13 | 24 | a |
3 | z | 12 | 23 | t | 15 | 26 | b |
4 | g | 16 | 27 | NA | NA | NA | c |
I want to know how to achieve this in R. Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
tidyr :: Pivot_wider
首先添加儿童的ID列:You could achieve your desired result using
tidyr::pivot_wider
by first adding an id column for the children: