I am using tidyverse to do some analysis to a dataset of home prices. I have home prices over time, and I would like to create new columns by applying a vector of down payments to output a new column for each down payment percentage.
set.seed(100)
dates <- seq.Date(from = as.Date('2010-01-01'), to = as.Date('2010-12-01'), by = 'months')
prices <- rnorm(12, mean = 100000, sd = 5000)
home_vals <- data.frame(dates, prices)
out <- home_vals %>%
mutate(
DownPayment_20 = 0.2 * prices
,DownPayment_10 = 0.1 * prices
,DownPayment_5 = 0.05 * prices
)
head(out)
dates prices DownPayment_20 DownPayment_10 DownPayment_5
1 2010-01-01 97489.04 19497.81 9748.904 4874.452
2 2010-02-01 100657.66 20131.53 10065.766 5032.883
3 2010-03-01 99605.41 19921.08 9960.541 4980.271
4 2010-04-01 104433.92 20886.78 10443.392 5221.696
5 2010-05-01 100584.86 20116.97 10058.486 5029.243
6 2010-06-01 101593.15 20318.63 10159.315 5079.658
I'd like to be able to do this for any number of down payment inputs, by just passing a vector like down_payments <- c(0.2, 0.1, 0.05), but I cannot figure out a straightforward way to do this without manually mutating every column. Additionally, I might want to include other vectors with inputs, and use both the down payment vector and the new vector in some calculation, and have a new column for that amount.
Thanks!