【问题标题】:how to reshape the matrix and fill the missing value as 0如何重塑矩阵并将缺失值填充为0
【发布时间】:2019-06-22 11:06:54
【问题描述】:

我有一个关于R中矩阵结构操作的问题,这里我需要先转置矩阵并将月份和状态列合并,用0填充缺失值。这里我有一个例子,目前我的数据如下所示。这似乎很棘手。如果有人能在这方面提供帮助,我将不胜感激。谢谢。

您好,我的数据如下所示:

  structure(list(Customer = c("1096261", "1096261", "1169502", 
    "1169502"), Phase = c("2", "3", "1", "2"), Status = c("Ontime", 
    "Ontime", "Ontime", "Ontime"), Amount = c(21216.32, 42432.65, 
    200320.05, 84509.24)), .Names = c("Customer", "Phase", "Status", 
    "Amount"), row.names = c(NA, -4L), class = c("grouped_df", "tbl_df", 
    "tbl", "data.frame"), vars = c("Customer", "Phase"), drop = TRUE, indices 
    = list(
    0L, 1L, 2L, 3L), group_sizes = c(1L, 1L, 1L, 1L), biggest_group_size = 1L, 
    labels = structure(list(
    Customer = c("1096261", "1096261", "1169502", "1169502"), 
    Phase = c("2", "3", "1", "2")), row.names = c(NA, -4L), class = 
    "data.frame", vars = c("Customer", 
    "Phase"), drop = TRUE, .Names = c("Customer", "Phase")))   

我需要具有以下列的重塑矩阵:
客户 Phase1earlyTotal Phase2earlyTotal....Phase4earlyTotal...Phase1_ Ontimetotal...Phase4_Ontimetotal...Phase1LateTotal_Phase4LateTotal。例如,Phase1earlytotal 包括 Phase=1 和 Status=Early 的金额之和。

目前我使用以下脚本,它不起作用,因为我不知道 如何结合相位和状态柱。

   mydata2<-data.table(mydata2,V3,V4)
    mydata2$V4<-NULL
    datacus <- data.frame(mydata2[-1,],stringsAsFactors = F); 
    datacus <- datacus %>% mutate(Phase= as.numeric(Phase),Amount= 
   as.numeric(Amount)) %>%
   complete(Phase = 1:4,fill= list(Amount = 0)) %>% 
   dcast(datacus~V3, value.var = 'Amount',fill = 0) %>% select(Phase, V3) 
   %>%t()

【问题讨论】:

  • 嗨,请provide code and data
  • 你可以使用dcast from reshape2 or data.table
  • 嗨,我的数据如下所示:客户阶段状态金额 1 1096261 2 Ontime 21216. 2 1096261 3 Ontime 42433. 3 1169502 1 Ontime 200320 . 4 1169502 2 准时 84509. 5 1169502 3 准时 863940. 6 1172386 1 准时 467078。
  • @Cherry 请更新您的问题并在此处包含dput(your_data) 的输出。请参阅@jay.sf 发布的链接以获取参考。

标签: r reshape data-manipulation


【解决方案1】:

我相信您正在寻找这样的想法?

样本数据

df <- structure(list(Customer = c("1096261", "1096261", "1169502", 
                            "1169502"), Phase = c("2", "3", "1", "2"), Status = c("Ontime", 
                                                                                  "Ontime", "Ontime", "Ontime"), Amount = c(21216.32, 42432.65, 
                                                                                                                            200320.05, 84509.24)), .Names = c("Customer", "Phase", "Status", 
                                                                                                                                                              "Amount"), row.names = c(NA, -4L), class = c("grouped_df", "tbl_df", 
                                                                                                                                                                                                           "tbl", "data.frame"), vars = c("Customer", "Phase"), drop = TRUE, indices 
          = list(
            0L, 1L, 2L, 3L), group_sizes = c(1L, 1L, 1L, 1L), biggest_group_size = 1L, 
          labels = structure(list(
            Customer = c("1096261", "1096261", "1169502", "1169502"), 
            Phase = c("2", "3", "1", "2")), row.names = c(NA, -4L), class = 
              "data.frame", vars = c("Customer", 
                                     "Phase"), drop = TRUE, .Names = c("Customer", "Phase")))   

#    Customer Phase Status    Amount
# 1:  1096261     2 Ontime  21216.32
# 2:  1096261     3 Ontime  42432.65
# 3:  1169502     1 Ontime 200320.05
# 4:  1169502     2 Ontime  84509.24

代码

library( data.table )
dcast( setDT( df ), Customer ~ Phase + Status, fun = sum, value.var = "Amount" )[]

输出

#    Customer 1_Ontime 2_Ontime 3_Ontime
# 1:  1096261        0 21216.32 42432.65
# 2:  1169502   200320 84509.24     0.00

【讨论】:

  • 您好,感谢您的回复。实际上我需要 12 列,客户 1_Ontime、2_Ontime、3_Ontime、4 Ontime、1 Late、2 Late、3 Late、4 Late、1 Early、2 Early、3 Early、4 Early,您能对此提出一些建议吗?
  • 非常感谢您的帮助。我认为这是解决问题的好方法。
  • @Cherry 您可能已经注意到,您需要的额外列是根据PhaseStatus 列中的唯一值自动创建的...因为仅样本数据包含 3 个独特的阶段和 1 个独特的状态,您最终会得到 Customer + 3 * 1 列。
  • 是的,完全正确。我注意到在回复您的 cmets 后,现在它可以正常工作了。非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多