【问题标题】:I want to change my data from long to wide format but there various variables [duplicate]我想将我的数据从长格式更改为宽格式,但是有各种变量[重复]
【发布时间】:2017-06-02 05:32:10
【问题描述】:

我当前的数据格式如下

ID  ID_2    Item ID Final Amount
001 111 1111    34623
001 111 1112    42567
001 112 1113    1254
001 112 1114    45237
001 112 1115    42913
001 112 1116    28117
001 113 1117    10312
008 222 1118    27367
008 222 1119    24714
008 223 1120    30949
011 333 1121    49529
012 444 1122    29762
012 444 1123    2743
012 444 1124    21357
012 444 1125    16256
012 444 1126    18376
017 555 1127    7877
017 555 1128    10684
017 555 1129    25281

但我希望它是一种矩阵

ID  ID_2    Item_1  Item_2  Item_3  Item_4  Item_5
1   111 34623   42567           
1   112 1254    45237   42913   28117   
1   113 10312               
8   222 27367   24714           
8   223 30949               
11  333 49529               
12  444 29762   2743    21357   16256   18376
17  555 7877    10684   25281       

谁能帮我解决这个问题?

【问题讨论】:

  • 在基础 R 中,您可以在创建项目计数变量后使用 reshape,如下所示:df1$times <- ave(df1$ItemID, df1$ID, df1$ID_2, FUN=seq_along),然后重塑宽 reshape(df1, direction="wide", idvar=c("ID", "ID_2"), drop="ItemID", v.names="times")
  • 错误:无法分配大小为 2.9 Gb 的向量

标签: r


【解决方案1】:

我们可以通过'ID','ID_2'创建一个序列列来做到这一点,使用dcast from data.table更容易

library(data.table)
dcast(setDT(df1), ID + ID_2 ~paste0("Item", rowid(ID, ID_2)), value.var = "FinalAmount")
#    ID ID_2 Item1 Item2 Item3 Item4 Item5
#1:  1  111 34623 42567    NA    NA    NA
#2:  1  112  1254 45237 42913 28117    NA
#3:  1  113 10312    NA    NA    NA    NA
#4:  8  222 27367 24714    NA    NA    NA
#5:  8  223 30949    NA    NA    NA    NA
#6: 11  333 49529    NA    NA    NA    NA
#7: 12  444 29762  2743 21357 16256 18376
#8: 17  555  7877 10684 25281    NA    NA

或者这可以通过tidyverse来完成

library(tidyverse)
df1 %>% 
   group_by(ID, ID_2) %>% 
   mutate(Seq = paste0("Item", row_number())) %>%
   select(-ItemID) %>%
   spread(Seq, FinalAmount)
# A tibble: 8 x 7
# Groups: ID, ID_2 [8]
#     ID  ID_2 Item1 Item2 Item3 Item4 Item5
#* <int> <int> <int> <int> <int> <int> <int>
#1     1   111 34623 42567    NA    NA    NA
#2     1   112  1254 45237 42913 28117    NA
#3     1   113 10312    NA    NA    NA    NA
#4     8   222 27367 24714    NA    NA    NA
#5     8   223 30949    NA    NA    NA    NA
#6    11   333 49529    NA    NA    NA    NA
#7    12   444 29762  2743 21357 16256 18376
#8    17   555  7877 10684 25281    NA    NA

数据

df1 <- structure(list(ID = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 8L, 8L, 8L, 
11L, 12L, 12L, 12L, 12L, 12L, 17L, 17L, 17L), ID_2 = c(111L, 
111L, 112L, 112L, 112L, 112L, 113L, 222L, 222L, 223L, 333L, 444L, 
444L, 444L, 444L, 444L, 555L, 555L, 555L), ItemID = 1111:1129, 
FinalAmount = c(34623L, 42567L, 1254L, 45237L, 42913L, 28117L, 
10312L, 27367L, 24714L, 30949L, 49529L, 29762L, 2743L, 21357L, 
16256L, 18376L, 7877L, 10684L, 25281L)), .Names = c("ID", 
"ID_2", "ItemID", "FinalAmount"), class = "data.frame", row.names = c(NA, 
 -19L))

【讨论】:

  • 无需在rowid imo 中包含ID;使用dcast(setDT(df1), ID + ID_2 ~ rowid(ID_2, prefix = 'Item'), value.var = "FinalAmount") 也可以
  • @Jaap 可以,但可能需要完整数据
  • 我的数据有超过 9L 行,所以它给出一个错误 Error: cannot allocate vector of size 58.4 Gb.
  • @dineshkhemani 这是因为您的系统没有内存来执行此操作。如果内存有问题,您可能必须在服务器上执行此操作
  • 我没有在服务器上执行此操作的选项。你能建议一个可以在系统内存上执行的解决方案吗?谢谢
猜你喜欢
  • 2017-10-13
  • 2012-10-16
  • 1970-01-01
  • 1970-01-01
  • 2020-10-23
  • 2016-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多