代码之家  ›  专栏  ›  技术社区  ›  Adela

数据中的应急表。框架柱

  •  1
  • Adela  · 技术社区  · 10 年前

    我正在尝试从我的数据集创建四向列联表。 我的数据集如下所示:

    a <- c(1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1)
    b <- c(1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1)
    group1 <- sample(letters[25:26], 12, replace = T)
    group2 <- sample(letters[7:10], 12, replace = T)
    
    df <- data.frame(a, b, group1, group2)
    

    aggregate 作用创建三向列联表时一切正常

    aggregate(cbind(a, b) ~ group1, data = df, FUN = table)
      group1 a.0 a.1 b.0 b.1
    1      y   3   4   3   4
    2      z   2   3   2   3
    

    然而,当添加第二个分组变量时,输出会令人困惑,而且不需要。

    aggregate(. ~ group1 + group2, data = df, FUN = table)
      group1 group2    a    b
    1      y      g    3    3
    2      z      g    1    1
    3      z      h    1    1
    4      y      i    1    1
    5      z      i    1    1
    6      y      j 2, 1    3
    7      z      j 1, 1 1, 1
    

    由于我的原始数据集相当大,我希望能用一些优雅、自动的方法来处理它。T

    2 回复  |  直到 10 年前
        1
  •  1
  •   akrun    10 年前

    melt/dcast

    library(data.table)
    dcast(melt(setDT(df), id.var = c("group1", "group2")), 
                           group1 + group2 ~variable + value, length)
    

    或者使用 recast (包装用于 熔融/dcast 从…起 reshape2 )

    library(reshape2)
    recast(df, measure.var = c("a", "b"), ... ~ variable + value, length)
    #    group1 group2 a_0 a_1 b_0 b_1
    #1      y      g   1   4   3   2
    #2      y      h   1   0   1   0
    #3      y      j   1   1   0   2
    #4      z      g   2   0   0   2
    #5      z      i   0   1   0   1
    #6      z      j   0   1   1   0
    

    OP公司 aggregate 给出这个输出

    aggregate(. ~ group1 + group2, data = df, FUN = table)
    #  group1 group2    a    b
    #1      y      g 1, 4 3, 2
    #2      z      g    2    2
    #3      y      h    1    1
    #4      z      i    1    1
    #5      y      j 1, 1    2
    #6      z      j    1    1
    

    如果我们想 总数的 获得这两个 levels factor 具有 水平 并执行 table

    do.call(data.frame, aggregate(cbind(a, b) ~ group1 + group2, data = df, 
                  FUN = function(x) table(factor(x, levels = 0:1))))
    #  group1 group2 a.0 a.1 b.0 b.1
    #1      y      g   1   4   3   2
    #2      z      g   2   0   0   2
    #3      y      h   1   0   1   0
    #4      z      i   0   1   0   1
    #5      y      j   1   1   0   2
    #6      z      j   0   1   1   0
    

    drop = FALSE 在里面 dcast

    dcast(melt(setDT(df), id.var = c("group1", "group2")), group1 + group2 ~
                       variable + value, length, drop = FALSE)
    

    或者在 改写

    recast(df, measure.var = c("a", "b"), ... ~ variable + value, length, drop = FALSE) 
    

    注意:没有 set.seed 对于 sample ,因此此处显示的输出与OP的输出不同

        2
  •  1
  •   Benjamin Mohn    10 年前

    可能有点复杂,但可能会有帮助,据我所知,你只是想数数,这样可以帮助:

    #Creating data
    a <- c(1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1)
    b <- c(1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1)
    group1 <- sample(letters[25:26], 12, replace = T)
    group2 <- sample(letters[7:10], 12, replace = T)
    df <- data.frame(a, b, group1, group2)
    
    # Counting variable a and b seperatly in a data frame
    counta <- xtabs( ~ group1 + group2 + a, data = df)
    countb <- xtabs( ~ group1 + group2 + b, data = df)
    df.a <- data.frame(counta)
    df.b <- data.frame(countb)
    
    #Now merging the data.frames:
    result.df <- merge(df.a, df.b, by.x= c("group1", "group2"),by.y=c("group1", "group2"), all = TRUE)
    
    # Result Looks like this:
    result.df
    
    #          group1 group2 a Freq.x    b Freq.y
    #   1       y      g     0      2    0      1
    #   2       y      g     0      2    1      1
    #   3       y      g     1      0    0      1
    #   4       y      g     1      0    1      1
    #   5       y      h     0      1    0      0
    #   6       y      h     0      1    1      1
    #   7       y      h     1      0    0      0
    #   8       y      h     1      0    1      1
    #   9       y      i     0      1    0      2
    #  10       y      i     0      1    1      1
    #  11       y      i     1      2    0      2
    #  12       y      i     1      2    1      1
    #  13       y      j     0      0    0      0
    #  14       y      j     0      0    1      0
    #  15       y      j     1      0    0      0
    #  16       y      j     1      0    1      0
    #  17       z      g     0      0    0      1
    #  18       z      g     0      0    1      0
    #  19       z      g     1      1    0      1
    #  20       z      g     1      1    1      0
    #  21       z      h     0      0    0      1
    #  22       z      h     0      0    1      1
    #  23       z      h     1      2    0      1
    #  24       z      h     1      2    1      1
    #  25       z      i     0      1    0      0
    #  26       z      i     0      1    1      1
    #  27       z      i     1      0    0      0
    #  28       z      i     1      0    1      1
    #  29       z      j     0      0    0      0
    #  30       z      j     0      0    1      2
    #  31       z      j     1      2    0      0
    #  32       z      j     1      2    1      2
    
    推荐文章