【发布时间】:2019-10-24 15:36:21
【问题描述】:
我正在尝试将数据集从长格式转换为宽格式。需要这样做才能输入另一个程序以进行分析。我的输入数据如下:
sdata <- data.frame(c(1,1,1,1,1,1,1,1,1,1,1,1,1),c(1,1,1,1,1,1,1,1,1,2,2,2,2),c("X1","A","B","C","D","X2","A","B","C","X1","A","B","C"),c(81,31,40,5,5,100,8,90,2,50,20,24,6))
col_headings <- c("Orig","Dest","Desc","Estimate")
names(sdata) <- col_headings
输入数据
根据上面 Orig-Dest-X1、Orig-Dest-X2 类别的独特组合,子类别仅从 A、B、C 到 A、B、C、D 到 A、B 等。我是试图获得所需的输出(在下面的 R 中重新创建的代码)以及所需输出的图像。
sdata_spread <- data.frame(c(1,1),c(1,2),c(81,50),c(31,20),c(40,24),c(5,6),c(5,NA),c(100,NA),c(8,NA),c(90,NA),c(2,NA))
col_headings <- c("Orig","Dest","X1", "X1_A", "X1_B", "X1_C", "X1_D","X2", "X2_A", "X2_B", "X2_C")
names(sdata_spread) <- col_headings
期望的输出
我尝试了以下方法:
sdata_spread <- sdata %>% spread(Desc,Estimate)
我得到的错误是:
Error: Each row of output must be identified by a unique combination of keys.
Keys are shared for 6 rows
我也尝试了这里给出的公认答案:Long to wide with no unique key 和这里:Long to wide format with several duplicates. Circumvent with unique combo of columns,但它没有得到我想要的输出。
任何见解都将不胜感激。
谢谢, 克里希南
【问题讨论】:
标签: r