# need a wrapper function to reset e <<- 0calc_line <-function(eq) { e <<-0eval_equation(eq)}equated <- equations %>%mutate(line_sum =map_dbl(V1, calc_line))# part oneas.character(sum(equated$line))
[1] "25190263477788"
part two
`/`<-function(a, b) { a + b}`-`<-function(a, b) { a * b}replace_symbol <-function(strng) {# swap precendence strng =str_replace_all(strng, "\\*", "-") strng =str_replace_all(strng, "\\+", "/")return(strng)}# test on individual functiontester <-"1 + 2 * 3 + 4 * 5 + 6"test2 <-replace_symbol(tester)eval(parse(text = test2))
[1] 231
# change the thing we want to calculateequate_part_two <- equations %>%mutate(line_2 =replace_symbol(V1), eq_2 =map_dbl(line_2, ~eval(parse(text = .))))# part twoas.character(sum(equate_part_two$eq_2))
make equal precedence by just replacing one of operators
`-`<-function(a, b) { a * b}replace_symbol <-function(strng) {# equal precendence strng =str_replace_all(strng, "\\*", "-")return(strng)}# test on individual functiontester <-"1 + 2 * 3 + 4 * 5 + 6"test1 <-replace_symbol(tester)eval(parse(text = test1))
[1] 71
# change the thing we want to calculateequate_part_one <- equations %>%mutate(line_1 =replace_symbol(V1), eq_1 =map_dbl(line_1, ~eval(parse(text = .))))# part oneas.character(sum(equate_part_one$eq_1))