Skip to content

Instantly share code, notes, and snippets.

@guychouk
Last active May 7, 2024 17:50
Show Gist options
  • Save guychouk/dc6f12f7c84ed5ad4c8ec1d774fe5067 to your computer and use it in GitHub Desktop.
Save guychouk/dc6f12f7c84ed5ad4c8ec1d774fe5067 to your computer and use it in GitHub Desktop.

Revisions

  1. guychouk revised this gist May 7, 2024. No changes.
  2. guychouk revised this gist May 7, 2024. No changes.
  3. guychouk created this gist Sep 28, 2021.
    40 changes: 40 additions & 0 deletions finances.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    # function that reverses a string by characters
    reverse_chars <- function(string)
    {
    # split string by characters
    string_split = strsplit(string, split = "")
    # reverse order
    rev_order = nchar(string):1
    # reversed characters
    reversed_chars = string_split[[1]][rev_order]
    # collapse reversed characters
    paste(reversed_chars, collapse = "")
    }

    # read a csv into a list
    finances2018 <- read.csv('./finances-2018.csv')

    # create a table from two data columns in the dataset.
    # by default, this gives the number of times each value appeared.
    table(finances2018$קטגוריה, finances2018$סכום.שחוייב)

    # replace the shekel sign with nothing in the סכום.שחוייב
    # column.
    gsub("", "", finances2018$סכום.שחוייב)

    # parse the סכום.שחוייב as numeric
    as.numeric(gsub("", "", finances2018$סכום.שחוייב))

    # generate two new lists:
    # amounts list as numbers and a categories list matching each value.
    amounts <- as.numeric(gsub(",", "", gsub("", "", finances2018$סכום.שחוייב)))
    # apply function to each value in list, similar to map()
    categories <- sapply(finances2018$קטגוריה, reverse_chars)
    # these two then get converted via an aggregate
    amountByGroup <- aggregate(x=list(Amounts=amounts), by=list(Categories=categories), FUN=sum)

    # create a pie chart from the
    png(file = "spendings.png")
    pie(amountByGroup$Amounts, amountByGroup$Categories)
    dev.off()