计算R中的小计 [英] Calculating subtotals in R

查看:100
本文介绍了计算R中的小计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Name of member Allowance Type             Expenditure Type  Date          Amount, £

Adam Afriyie Office running costs (IEP/AOE) Incidentals     07/03/2009 111.09
Adam Afriyie Office running costs (IEP/AOE) Incidentals     11/05/2009 111.09
Adam Afriyie Office running costs (IEP/AOE) Incidentals     11/05/2009 51.75
Adam Holloway   Office running costs (IEP/AOE)  Incidentals  10/01/2009  35
Adam Holloway   Office running costs (IEP/AOE)  Incidentals  10/01/2009  413.23
Adam Holloway   Office running costs (IEP/AOE)  Incidentals  10/01/2009  9.55
Adam Holloway   Office running costs (IEP/AOE   IT equipment 07/03/2009 890.01
Adam Holloway   Communications Expenditure   Publications   12/04/2009  1774
Adam Holloway   Office running costs (IEP/AOE)  Incidentals  12/08/2009  1.1
Adam Holloway   Office running costs (IEP/AOE   Incidentals  12/08/2009  64.31
Adam Holloway   Office running costs (IEP/AOE)  Incidentals  12/08/2009  64.31

R是您的新手,也是编程的新手。这是国会议员在一定时期内支出的一部分。我想对每位国会议员的费用进行小计,并使用了另一篇文章中的代码

Hi im new to R and new to programming. This is a subset of the MP's expenses during a certain time period. I want to subtotal each MP's expenses and i used the code from another post

> aggregate(cbind(bsent, breturn, tsent, treturn, csales) ~ yname, data = foo, 
 +           FUN = sum)

并根据自己的情况进行编辑。

and edited it to my own situation.

我的代码:

expenses2 <- aggregate(cbind(Amount..Â.) ~ Name.of.member, data = expenses, FUN = sum)

现在,尽管此代码确实进行了某种汇总,但数字却不匹配。例如,可以计算出亚当·阿夫里耶(Adam Afriyie)的费用为273.93英镑,但是此代码得出的结果为12697。我不知道这个数字代表什么。有人可以帮助我,告诉我我做错了什么吗?

now although this code does do some sort of aggregation the numbers do not match up. for example one can calculate that Adam Afriyie's expenses are £273.93 however this code gives a result of 12697. I have no idea what this number represents. Can someone help me and tell me what im doing wrong??

预先感谢您

推荐答案

我将文本拖到编辑。然后设置有效的标题名称,并放回显然已被空格替换的选项卡,并读入R以获取该对象:

I pulled that text into an editor. Then made valid header names and put back the tabs that had apparently been replaced with spaces and read into R getting this object:

    MPexp <- structure(list(Name_of_member = c("Adam Afriyie", "Adam Afriyie", 
    "Adam Afriyie", "Adam Holloway", "Adam Holloway", "Adam Holloway", 
    "Adam Holloway", "Adam Holloway", "Adam Holloway", "Adam Holloway", 
    "Adam Holloway"), Allowance_Type = c("Office running costs (IEP/AOE)", 
    "Office running costs (IEP/AOE)", "Office running costs (IEP/AOE)", 
    " Office running costs (IEP/AOE)", " Office running costs (IEP/AOE)", 
    " Office running costs (IEP/AOE)", " Office running costs (IEP/AOE", 
    " Communications Expenditure", " Office running costs (IEP/AOE)", 
    " Office running costs (IEP/AOE", " Office running costs (IEP/AOE)"
    ), Expenditure_Tyoe = c("Incidentals", "Incidentals", "Incidentals", 
    "Incidentals", "Incidentals", "Incidentals", "IT equipment", 
    "Publications", "Incidentals", "Incidentals", "Incidentals"), 
        Date = c("07/03/09", "11/05/09", "11/05/09", "10/01/09", 
        "10/01/09", "10/01/09", "07/03/09", "12/04/09", "12/08/09", 
        "12/08/09", "12/08/09"), Amount = c(111.09, 111.09, 51.75, 
        35, 413.23, 9.55, 890.01, 1774, 1.1, 64.31, 64.31)), .Names = c("Name_of_member", 
    "Allowance_Type", "Expenditure_Tyoe", "Date", "Amount"), 
class = "data.frame", row.names = c(NA, 
    -11L))

现在,这应该会产生预期的结果,总计如下:

Now this should yield the expected result with aggregate:

> aggregate(MPexp$Amount, MPexp["Name_of_member"], sum)
  Name_of_member       x
1   Adam Afriyie  273.93
2  Adam Holloway 3251.51

再次阅读您的问题使我意识到您正在使用aggregate.formula,因此这也适用于该数据:

Reading your question again made me realize that you were using aggregate.formula so this would also work on that data:

> aggregate(Amount ~ Name_of_member, data=MPexp, FUN=sum)
  Name_of_member  Amount
1   Adam Afriyie  273.93
2  Adam Holloway 3251.51

这篇关于计算R中的小计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆