此问题已在此处有答案:
How to reshape data from long to wide format(14个回答)
两年前关闭。
我的数据格式为:
Sender Action Recipient Operation
Sender1 Update Recipient3 Operation1
Sender2 Update Recipient4 Operation2
Sender3 Update Recipient5 Operation3
Sender1 Update Recipient6 Operation1
Sender2 Delete Recipient3 Operation4
Sender3 Delete Recipient4 Operation5
Sender1 Update Recipient5 Operation1
Sender2 Delete Recipient6 Operation4
Sender1 Delete Recipient3 Operation6
字符串
我希望我的数据采用以下格式,每个操作都显示在一行中,并且根据与操作绑定的收件人数量动态更新列
Operation User1 Action User2 User3 User4
Operation1 Sender1 Update Recipient3 Recipient6 Recipient5
Operation2 Sender2 Update Recipient4
Operation3 Sender3 Update Recipient5
Operation4 Sender2 Delete Recipient3 Recipient6
Operation5 Sender3 Delete Recipient4
Operation6 Sender1 Delete Recipient3
型
如何在R中实现这一点?
3条答案
按热度按时间bvn4nwqk1#
对于那些习惯并喜欢data.table语法的人来说,一个(可能不是最佳的)版本可能看起来像这样:
字符串
6ojccjat2#
您可以使用
pivot_wider
来获取更宽格式的数据。字符串
数据
型
vjhs03f73#
使用
reshape
。字符串
或
reshape2::dcast
。型
型