添加一行Sum和列的平均值

时间:2011-08-26 08:49:02

标签: r

我的数据框如下所示。

    `> am_me
      Group.1         Group.2 x.x       x.y
    2      AM  clearterminate   3  21.00000
    3      AM display.cryptic  86  30.12791
    4      AM           price  71 898.00000`

我想得到如下结果。

    `> am_me_t
               Group.2 x.x              x.y
    2   clearterminate   3               21
    3  display.cryptic  86 30.1279069767442
    4            price  71              898
    41            AM 160 316.375968992248`

我已取出第一列并得到如下结果

    `> am_res
              Group.2 x.x       x.y
    2  clearterminate   3  21.00000
    3 display.cryptic  86  30.12791
    4           price  71 898.00000`

当我尝试rbind将“AM”添加到新行时,如下所示,我收到一条警告消息并获得NA。

     `> am_me_t <- rbind(am_res, c("AM", colSums(am_res[2]), colMeans(am_res[3])))
    Warning message:
    invalid factor level, NAs generated in: "[<-.factor"(`*tmp*`, ri, value = "AM")

               Group.2 x.x              x.y
    2   clearterminate   3               21
    3  display.cryptic  86 30.1279069767442
    4            price  71              898
    41            <NA> 160 316.375968992248`

有关您的信息,请输出编辑(am_me)

    `> edit(am_me)
    structure(list(Group.1 = structure(as.integer(c(2, 2, 2)), .Label = c("1Y",
    "AM", "BE", "CM", "CO", "LX", "SN", "US", "VK", "VS"), class = "factor"),
        Group.2 = structure(as.integer(c(2, 5, 9)), .Label = c("bestbuy",
        "clearterminate", "currency.display", "display", "display.cryptic",
        "fqa", "mileage.display", "ping", "price", "reissue", "reissuedisplay",
        "shortaccess.followon"), class = "factor"), x.x = as.integer(c(3,
        86, 71)), x.y = c(21, 30.1279069767442, 898)), .Names = c("Group.1",
    "Group.2", "x.x", "x.y"), row.names = c("2", "3", "4"), class = "data.frame")`

另外

    `> edit(me)
    structure(list(Group.1 = structure(as.integer(c(1, 2, 2, 2, 3,
    4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 8, 8,
    8, 8, 9, 9, 10, 10, 10, 10, 10, 10)), .Label = c("1Y", "AM",
    "BE", "CM", "CO", "LX", "SN", "US", "VK", "VS"), class = "factor"),
        Group.2 = structure(as.integer(c(8, 2, 5, 9, 10, 1, 2, 5,
        9, 1, 2, 5, 9, 1, 2, 3, 4, 7, 9, 11, 12, 2, 4, 6, 1, 2, 5,
        9, 2, 5, 1, 2, 3, 5, 9, 10)), .Label = c("bestbuy", "clearterminate",
        "currency.display", "display", "display.cryptic", "fqa",
        "mileage.display", "ping", "price", "reissue", "reissuedisplay",
        "shortaccess.followon"), class = "factor"), x.x = as.integer(c(1,
        3, 86, 71, 1, 2, 5, 1, 52, 10, 7, 27, 15, 5, 267, 14, 4,
        1, 256, 1, 1, 80, 1, 78, 2, 10, 23, 6, 1, 2, 4, 3, 3, 11,
        1, 1)), x.y = c(5, 21, 30.1279069767442, 898, 12280, 800,
        56.4, 104, 490.442307692308, 1759.1, 18.1428571428571, 1244.81481481481,
        518.533333333333, 3033.2, 18.5468164794007, 20, 3788.5, 23,
        2053.49609375, 3863, 6376, 17.825, 240, 1752.21794871795,
        1114.5, 34, 1369.60869565217, 1062.16666666667, 23, 245,
        5681.5, 11.3333333333333, 13.3333333333333, 1273.81818181818,
        2076, 5724)), .Names = c("Group.1", "Group.2", "x.x", "x.y"
    ), row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9",
    "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
    "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31",
    "32", "33", "34", "35", "36"), class = "data.frame")

       Group.1              Group.2 x.x         x.y
    1       1Y                 ping   1     5.00000
    2       AM       clearterminate   3    21.00000
    3       AM      display.cryptic  86    30.12791
    4       AM                price  71   898.00000
    5       BE              reissue   1 12280.00000
    6       CM              bestbuy   2   800.00000
    7       CM       clearterminate   5    56.40000
    8       CM      display.cryptic   1   104.00000
    9       CM                price  52   490.44231
    10      CO              bestbuy  10  1759.10000
    11      CO       clearterminate   7    18.14286
    12      CO      display.cryptic  27  1244.81481
    13      CO                price  15   518.53333
    14      LX              bestbuy   5  3033.20000
    15      LX       clearterminate 267    18.54682
    16      LX     currency.display  14    20.00000
    17      LX              display   4  3788.50000
    18      LX      mileage.display   1    23.00000
    19      LX                price 256  2053.49609
    20      LX       reissuedisplay   1  3863.00000
    21      LX shortaccess.followon   1  6376.00000
    22      SN       clearterminate  80    17.82500
    23      SN              display   1   240.00000
    24      SN                  fqa  78  1752.21795
    25      US              bestbuy   2  1114.50000
    26      US       clearterminate  10    34.00000
    27      US      display.cryptic  23  1369.60870
    28      US                price   6  1062.16667
    29      VK       clearterminate   1    23.00000
    30      VK      display.cryptic   2   245.00000
    31      VS              bestbuy   4  5681.50000
    32      VS       clearterminate   3    11.33333
    33      VS     currency.display   3    13.33333
    34      VS      display.cryptic  11  1273.81818
    35      VS                price   1  2076.00000
    36      VS              reissue   1  5724.00000`

1 个答案:

答案 0 :(得分:3)

Group.2列的类型是factor,它限制了可能的值。您可以使用am_me$Group.2 <- as.character(am_me$Group.2)将其转换为字符,之后将添加AM值而不会出错。

请注意,您还可以使用sum()mean()进行单列操作。