代码之家  ›  专栏  ›  技术社区  ›  Andrew Brēza

按R中的索引变量对行求和

  •  0
  • Andrew Brēza  · 技术社区  · 14 年前

    我正在使用美国所有交通系统的数据库,并试图比较不同的机构。每个案例都是组织的一个特定部分。例如,公交线路与地铁是分开的。我想将给定机构的所有案例的价值结合起来。

    基本上,我想对每个“Trs_Id”的每一列的值求和,然后删除其余的。这个数据框架是运营费用的细分(“opex”)。以下是我的数据集在R中的样子:

    > colnames(opex)
    [1] "Trs_Id"                  "Mode_Cd"                
    [3] "Service_Cd"              "Expense_Category_Desc"  
    [5] "Op_Sal_Wage_Amt"         "Other_Sal_Wage_Amt"     
    [7] "Fringe_Benefit_Amt"      "Service_Costs_Amt"      
    [9] "Fuel_Lubricant_Amt"      "Tire_Tube_Amt"          
    [11] "Other_Mat_Sup_Amt"       "Utility_Amt"            
    [13] "Casuality_Liability_Amt" "Tax_Amt"                
    [15] "In_Report_Amt"           "Sep_Report_Amt"         
    [17] "Misc_Expense_Amt"        "Expense_Transfer_Amt"   
    [19] "Ada_Related_Amt"        
    > NROW(opex)
    [1] 6956
    > id_nm <- table(opex$Trs_Id)
    > NROW(id_nm)
    [1] 616
    
    2 回复  |  直到 14 年前
        1
  •  4
  •   Jilber Urbina    14 年前

    尽管你没有制作一个可重复的例子来证明你的问题,但我认为你可以使用 ddply 函数来自 plyr 包,但您也可以使用基本函数 aggregate .我更喜欢用 ddply公司 。

    以下是一些用于计算数据的随机数。frame

     #install.packages('plyr')
     library(plyr)
    
     set.seed(007) # for the example being reproducible
     Mode_Cd1 <- replicate(4,sample(LETTERS[1:26], 1, replace=T)) # random genereation of variable Mode_Cd
     Mode_Cd2 <- replicate(4,sample(LETTERS[1:26], 1, replace=T)) 
    
     data_set <- data.frame(Trs_Id = rep(paste('00', 1:4, sep=''), each=3),
                Mode_Cd = sample(paste(Mode_Cd1, Mode_Cd2, sep=''), 12, replace=T),
                Service_Cd =  sample(paste(Mode_Cd2, Mode_Cd1, sep=''), 12, replace=T),
                Op_Sal_Wage_Amt = rnorm(12,5000,100),
                Other_Sal_Wage = rnorm(12,3000,800))
    
     data_set # this is how my random data_set looks like
       Trs_Id Mode_Cd Service_Cd Op_Sal_Wage_Amt Other_Sal_Wage
    1     001      ZG         ID        4910.620       2213.558
    2     001      KU         UK        4969.267       2779.149
    3     001      ZG         ZB        4999.518       2303.319
    4     002      ZG         ZB        5098.816       3574.968
    5     002      BZ         ZB        5083.975       3088.522
    6     002      ZG         GZ        5070.534       2937.227
    7     003      KU         ID        5130.596       2663.608
    8     003      ZG         UK        4861.200       2550.299
    9     003      DI         ZB        5127.292       3798.011
    10    004      ZG         UK        5018.419       2115.896
    11    004      BZ         ID        5075.228       2886.170
    12    004      KU         UK        5059.175       3251.996
    
     ddply(data_set,.(Trs_Id),numcolwise(sum)) # The sum you want.
      Trs_Id Op_Sal_Wage_Amt Other_Sal_Wage
    1    001        14879.40       7296.026
    2    002        15253.33       9600.717
    3    003        15119.09       9011.918
    4    004        15152.82       8254.062
    
        2
  •  2
  •   dcarlson    14 年前

    使用Jilber的数据集,您还可以使用aggregate()处理此问题:

    aggregate(data_set[, 4:5], list(data_set$Trs_Id), sum)
    

    对于您的示例,列编号将为5:19,而不是4:5。