Q:

Calculate net income

I have a dataset looks like :
            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

I want to calculate net income by date where if you have income more than outcome result will be positive else negative.
The result should look like this:
       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
    ...

How can I get this efficiently?

A:

Sample data:
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))

Transform time to date and outcome in negative values:
df$date <- lubridate::date(df$time)
df$amount[df$type=="outcome"] <- df$amount[df$type=="outcome"]*-1

Use dplyr to summarise the data (sum of amount by date):
library(dplyr)

output <- df %>% group_by(date) %>% summarise(netincome=sum(amount)) 

Result:
output

# A tibble: 2 x 2
      date netincome
     <chr>     <dbl>
1 2017/1/1    738.72
2 2017/1/2  -1456.07

