使用dcast重塑R中的数据

时间:2018-06-18 14:18:27

标签: r dcast

我有以下数据

library(reshape2)

d <- data_frame(
            Quart = c("Q1", "Q2", "Q3", "Q4", "Q4", "Q1","Q4","Q1", "Q2", "Q3"),
            var1 = c(1,0,1,1,1,0,2,3,0,1),
            var2= c(1,0,1,1,1,0,1,0,1,0),
            location= c("loc1", "loc1", "loc2", "loc2", "loc2", "loc2", "loc2",
                        "loc1", "loc2","loc1"))

我想重塑这些数据以创建一个如下所示的表格。

加星标的单元格应该是var1乘以其中Quart = Q1的loc1的数量。所以在这种情况下它将是'3',loc2将是'0',因为没有任何行同时具有Q1和loc2,loc3将是'1',并且loc4将是'0'。

enter image description here

我尝试使用d cast重新整形数据:

  d%>%
  reshape2::dcast(formula = Quart * var1 ~ location
              ,fun.aggregate = length       
translates to counts
              ,margins = c("Quart", "var1")
  ) -> d

然而,这并没有给我我想要的东西。有什么想法吗?

由于

1 个答案:

答案 0 :(得分:1)

选项是首先使用tidyr::gather以长格式转换var1var2,然后将dcast应用为:

library(reshape2)
library(tidyverse)
gather(d, key, value, -Quart, -location) %>%
  dcast(Quart+key ~ location, fun.aggregate = sum, value.var = "value")

#   Quart  key loc1 loc2
# 1    Q1 var1    4    0
# 2    Q1 var2    1    0
# 3    Q2 var1    0    0
# 4    Q2 var2    0    1
# 5    Q3 var1    1    1
# 6    Q3 var2    0    1
# 7    Q4 var1    0    4
# 8    Q4 var2    0    3