计算 R 中的小计

发布于 2024-10-05 15:15:59 字数 1457 浏览 4 评论 0原文

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 新手,也是编程新手。这是国会议员在特定时期内开支的一部分。我想小计每个议员的费用,我使用了另一篇文章中的代码

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

并将其编辑为我自己的情况。

我的代码:

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

现在虽然此代码确实进行了某种聚合,但数字不匹配。例如,可以计算出 Adam Afriyie 的费用为 273.93 英镑,但此代码给出的结果为 12697。我不知道这个数字代表什么。有人可以帮助我并告诉我我做错了什么吗?

先感谢您

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

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.

my code:

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

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??

Thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自演自醉 2024-10-12 15:15:59

我将该文本拖入编辑器中。然后制作有效的标题名称并放回显然已被空格替换的选项卡并读入 R 获取此对象:

    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))

现在这应该会产生聚合的预期结果:

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

再次阅读你的问题让我意识到你正在使用aggregate.formula所以这也适用于该数据:

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

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

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
对你而言 2024-10-12 15:15:59

仅使用您的姓名列和最后金额列:

df <- data.frame(name = c(rep("Adam Afriyie", 3), rep("Adam Holloway", 8)),
                 amount = c(111.09, 111.09, 51.75, 35,
                   413.23, 9.55, 890.01, 1774, 1.1, 64.31, 64.31)
                 )

版本 1

aggregate(df$amount, by = list(name = df$name), FUN = "sum")

版本 2

aggregate(amount ~ name, data = df, FUN = "sum")

输出:

1  Adam Afriyie  273.93
2  Adam Holloway 3251.51

Using only your name column and your last amount column:

df <- data.frame(name = c(rep("Adam Afriyie", 3), rep("Adam Holloway", 8)),
                 amount = c(111.09, 111.09, 51.75, 35,
                   413.23, 9.55, 890.01, 1774, 1.1, 64.31, 64.31)
                 )

version 1

aggregate(df$amount, by = list(name = df$name), FUN = "sum")

version 2

aggregate(amount ~ name, data = df, FUN = "sum")

output:

1  Adam Afriyie  273.93
2  Adam Holloway 3251.51
马蹄踏│碎落叶 2024-10-12 15:15:59

另一种使用 plyr 的方法

library(plyr)

#Using data from mropa's answer
> ddply(df, .(name), summarise, sum = sum(amount))
           name     sum
1  Adam Afriyie  273.93
2 Adam Holloway 3251.51

Another approach using plyr

library(plyr)

#Using data from mropa's answer
> ddply(df, .(name), summarise, sum = sum(amount))
           name     sum
1  Adam Afriyie  273.93
2 Adam Holloway 3251.51
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文