Last active
January 24, 2021 16:26
-
-
Save gadenbuie/ec91e04a9dc82de7f696f397c1f55ab1 to your computer and use it in GitHub Desktop.
This file contains 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 characters
library(tidyverse) | |
recipes <- tibble::tribble( | |
~title, ~ingredients, | |
"Banana Bread", "4 bananas", | |
"Banana Bread", "1/2 cup butter", | |
"Banana Bread", "2/3 cup sugar", | |
"Banana Bread", "2 eggs", | |
"Banana Bread", "2 cups flour", | |
"Banana Bread", "1/4 tsp baking soda" | |
) | |
# π¨ IMPORTANT! π¨ | |
recipes %>% # β β Find (number)/(number) | |
mutate( # βββ β | |
ingredients_new = str_replace_all(ingredients, "\\d+/\\d+", function(x) eval(parse(text = x))) | |
) # βββββββββββββββββββββββββββββββββ | |
# β | |
# Evaluate as code to divide and replace in the string β | |
#> # A tibble: 6 x 3 | |
#> title ingredients ingredients_new | |
#> <chr> <chr> <chr> | |
#> 1 Banana Bread 4 bananas 4 bananas | |
#> 2 Banana Bread 1/2 cup butter 0.5 cup butter | |
#> 3 Banana Bread 2/3 cup sugar 0.666666666666667 cup sugar | |
#> 4 Banana Bread 2 eggs 2 eggs | |
#> 5 Banana Bread 2 cups flour 2 cups flour | |
#> 6 Banana Bread 1/4 tsp baking soda 0.25 tsp baking soda |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment