I have a data frame with the following columns:
sales_1, sales_2, price_1, price_2
Now, I want to calculate revenues by multiplying sales_1 * price_1 and so-on across each number in an iterative manner.
Below is my sample data
sample_data <- data_frame(
"sales_1" = c(10, 20, 30),
"sales_2" = c(20, 30, 40),
"price_1" = c(30, 20, 20),
"price_2" = c(30, 30, 50))
sample_data
# A tibble: 3 x 4
# sales_1 sales_2 price_1 price_2
# <dbl> <dbl> <dbl> <dbl>
#1 10 20 30 30
#2 20 30 20 30
#3 30 40 20 50
I have tried the following code. But this does not work.
sample_data %>%
mutate (
for (i in seq_along(1:2)) {
paste0("total_revenue",i) = paste0("sales_",i) * paste0("price_",i)
}
)