Last active
May 7, 2024 17:50
-
-
Save guychouk/dc6f12f7c84ed5ad4c8ec1d774fe5067 to your computer and use it in GitHub Desktop.
Revisions
-
guychouk revised this gist
May 7, 2024 . No changes.There are no files selected for viewing
-
guychouk revised this gist
May 7, 2024 . No changes.There are no files selected for viewing
-
guychouk created this gist
Sep 28, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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()