sqldf中按组累计的总和?
我有一个包含 3 个变量的数据框:地点、时间和值 (P、T、X)。我想创建第四个变量,它将是 X 的累积和。通常我喜欢使用 sqldf 进行分组计算,但似乎找不到 cumsum 的等效项>。那就是:
sqldf("select P,T,X, cumsum(X) as X_CUM from df group by P,T")
不起作用。这对于 sqldf
是否可行?我尝试了doBy
,但这也不全是cumsum
。
I have a data frame with 3 variables: place, time, and value (P, T, X). I want to create a fourth variable which will be the cumulative sum of X. Normally I like to do grouping calculations with sqldf
, but can't seem to find an equivalent for cumsum
. That is:
sqldf("select P,T,X, cumsum(X) as X_CUM from df group by P,T")
doesn't work. Is this even possible with sqldf
? I tried doBy
, but that doesn't all cumsum
either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
设置一些测试数据:
现在我们有三种解决方案。首先,我们根据要求使用 sqldf,使用默认的 SQLite 数据库。接下来我们再次使用 sqldf 进行此操作,但这次使用 PostgreSQL 使用 RPostgreSQL 或 RpgSQL 驱动程序。 PostgreSQL 支持分析窗口函数,可以简化 SQL。您需要首先设置一个 PostgreSQL 数据库才能执行此操作。最后,我们展示了一个仅使用 R 核心的纯 R 解决方案。
1) sqldf/RSQLite
2) sqldf/RPostgreSQL
(这也适用于 RpgSQL PostgreSQL 驱动程序。使用您必须安装 Java 并设置 PostgreSQL 数据库,然后代替上述使用:
1ibrary(RpgSQL); sqldf(...)
其中使用相同的 SQL 字符串,但应该是DF
周围没有引号。)3) Plain R
Set up some test data:
and now we have three solutions. First we use sqldf, as requested, using the default SQLite database. Next we do it with sqldf again but this time with PostgreSQL using RPostgreSQL or RpgSQL driver. PostgreSQL supports analytical windowing functions which simplify the SQL. You will need to set up a PostgreSQL database first to do that one. Finally we show a pure R solution which only uses the core of R.
1) sqldf/RSQLite
2) sqldf/RPostgreSQL
(This also works with the RpgSQL PostgreSQL driver. To use that you must have Java installed and a PostgreSQL database set up and then in place of the above use:
1ibrary(RpgSQL); sqldf(...)
where the same SQL string is used except there should be no quotes aroundDF
.)3) Plain R
我希望我明白你想要什么:
这对你有帮助吗?
I hope i understood what you want:
does this help you?
或者,另一个选项是 data.table。
Or, another option is data.table.