library('pacman') p_load('stringr') p_load('httr') p_load('crayon') year=commandArgs(TRUE)[1] mon=commandArgs(TRUE)[2] df <- read.table("2018.csv", header=FALSE, sep=",", row.names=NULL) df = df[c(-2,-6,-7,-8)] colnames(df) <- c("dates", "description", "amount", "balance") df$dates = as.Date(df$dates, "%d/%m/%Y") df$amount = abs(as.numeric(df$amount)) df$balance = as.numeric(df$balance) head(df) # read transactions file #df = read.csv('./2018.csv', header=F, sep=',') # create colunm names #names(df) <- c("dates", "counterparty", "amount", "balance") # remove negative amounts and make numeric #df$amount = abs(as.numeric(df$amount)) #df$balance = as.numeric(df$balance) #turn into dates format #df$dates = as.Date(df$dates, "%Y-%m-%d") # create empty colunm to populate manually df$category <- NA # only select transactions from relevant month month = subset(df, format.Date(dates, "%m")==mon & format.Date(dates, "%Y")==year) # order rows by latest date first month = month[ order(month$date , decreasing = FALSE ),] #print(month) # write each row to individual file for (i in seq(1, nrow(month))){ write.csv(month[i,], file=paste("./",year,"/",mon,"/",sprintf("%03d",i),".csv", sep=""), row.names=FALSE, quote=FALSE) } p_load(plyr) mon=sprintf("%02s",mon) file_list <- list.files(path=gsub(" ","",paste('./',year,'/',mon,'/tagged/')), pattern="*.csv") df <- do.call("rbind", lapply(file_list, function(x) read.csv(paste('./',year,'/',mon,'/tagged/', x, sep=''), stringsAsFactors = FALSE))) data <- data.frame( year = year, month = mon, rent = sum(df[df$category == "RENT",]$amount,na.rm=TRUE), food = round(sum(df[df$category == "FOOD",]$amount,na.rm=TRUE),digits=0), other = round(sum(df[df$category == "OTHER",]$amount,na.rm=TRUE),digits=0), cash = round(sum(df[df$category == "CASH",]$amount,na.rm=TRUE),digits=0)) data$expenses = data$rent + data$food + data$other + data$cash data$net_wage = round(sum(month[month$counterparty == "SCOTCH PARTN",]$amount),digits=0) data$profit = data$net_wage - data$expenses data$balance = tail(month, 1)$balance print(data)