Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中数据的动态透视_R_Dplyr_Tidyverse - Fatal编程技术网

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

我希望在R中动态地将表旋转得更宽

考虑以下示例:

+---------+-------+---------+
| 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))