R 计算净收入
我有一个数据集,看起来像:R 计算净收入,r,dataframe,dplyr,R,Dataframe,Dplyr,我有一个数据集,看起来像: time type amount 1 2017/1/1 0:00 income 729.64 2 2017/1/1 0:05 income 1465.15 3 2017/1/1 0:10 outcome 1456.07 4 2017/1/1 0:15 outcome 1764.28 ... 289 2017/1/2 0:00 income 719.64 290 201
time type amount
1 2017/1/1 0:00 income 729.64
2 2017/1/1 0:05 income 1465.15
3 2017/1/1 0:10 outcome 1456.07
4 2017/1/1 0:15 outcome 1764.28
...
289 2017/1/2 0:00 income 719.64
290 2017/1/2 0:05 income 165.15
291 2017/1/2 0:10 income 1006.07
292 2017/1/2 0:15 outcome 104.28
我想按日期计算净收入,若你们的收入超过结果,结果将为正,否则为负。
结果应该如下所示:
date netincome
1 2017/1/1 -729.64
2 2017/1/2 1465.15
3 2017/1/3 1456.07
4 2017/1/4 1764.28
...
如何有效地获取此信息?示例数据:
df <- data.frame(time=c("2017/1/1 0:00", "2017/1/1 0:05", "2017/1/1 0:10","2017/1/2 0:00", "2017/1/2 0:05", "2017/1/2 0:10"),
type=c("income", "income", "outcome", "income", "outcome", "outcome"),
amount=c(729.64, 1465.15, 1456.07, 729.64, 729.64, 1456.07))
使用dplyr按日期汇总金额的数据总和:
结果:
output
# A tibble: 2 x 2
date netincome
<chr> <dbl>
1 2017/1/1 738.72
2 2017/1/2 -1456.07
其他解决办法可以是:
library(tidyverse)
library(lubridate)
df %>%
spread(type, amount) %>%
group_by(date = date(time)) %>%
summarise(netincome = sum(income, na.rm = TRUE) - sum(outcome, na.rm = TRUE))
# # A tibble: 2 x 2
# date netincome
# <date> <dbl>
# 1 2017-01-01 739
# 2 2017-01-02 -1456
output
# A tibble: 2 x 2
date netincome
<chr> <dbl>
1 2017/1/1 738.72
2 2017/1/2 -1456.07
library(tidyverse)
library(lubridate)
df %>%
spread(type, amount) %>%
group_by(date = date(time)) %>%
summarise(netincome = sum(income, na.rm = TRUE) - sum(outcome, na.rm = TRUE))
# # A tibble: 2 x 2
# date netincome
# <date> <dbl>
# 1 2017-01-01 739
# 2 2017-01-02 -1456