R中数据的动态透视
我希望在R中动态地将表旋转得更宽 考虑以下示例:R中数据的动态透视,r,dplyr,tidyverse,R,Dplyr,Tidyverse,我希望在R中动态地将表旋转得更宽 考虑以下示例: +---------+-------+---------+ | GroupID | Store | Price | +---------+-------+---------+ | G1 | S1 | $1.00 | | G1 | S2 | $2.00 | | G1 | S3 | $3.00 | | G1 | S4 | $4.00 | | G1 | S5
+---------+-------+---------+
| GroupID | Store | Price |
+---------+-------+---------+
| G1 | S1 | $1.00 |
| G1 | S2 | $2.00 |
| G1 | S3 | $3.00 |
| G1 | S4 | $4.00 |
| G1 | S5 | $5.00 |
| G2 | S6 | $6.00 |
| G2 | S7 | $7.00 |
| G3 | S8 | $8.00 |
+---------+-------+---------+
我希望对其进行转换,使每一行在GroupID上都是唯一的,并且Store和Price作为列连续动态地遍历。我一直在尝试Tidyverse中的pivot_加宽功能,但我一直在研究如何制作这些列
结果:
+---------+--------+----------+--------+----------+--------+----------+--------+----------+--------+----------+
| GroupID | Store1 | Price1 | Store2 | Price2 | Store3 | Price3 | Store4 | Price4 | Store5 | Price5 |
+---------+--------+----------+--------+----------+--------+----------+--------+----------+--------+----------+
| G1 | S1 | $1.00 | S2 | $2.00 | S3 | $3.00 | S4 | $4.00 | S5 | $5.00 |
| G2 | S6 | $6.00 | S7 | $7.00 | | | | | | |
| G3 | S8 | $8.00 | | | | | | | | |
+---------+--------+----------+--------+----------+--------+----------+--------+----------+--------+----------+
df%%>%groupby(GroupID)%%>%mutate(row=row\u number())%%>%pivot\u wide(names\u from=row,values\u from=c(Store,Price))