0

I'm looking to take 4 vectors (Year, Type, Update, Percent) and define all possible combinations (modscombo) and then apply these combinations of values across a master vector of values (location) as a multiplier.

Example:

year <- c("Pre-95", "95-01", "02-13")
type <- c("A","B","C")
update <- c("Pre-08", "08-13", "14-18")
percent <- c(1,2,3)
modifiers <- data.frame(year, type, update, percent)
modscombo <- expand.grid(modifiers)

...once I have each combination of modifiers... I would like to apply each modifier to the each of the location values below:

area <- c(12345,67890,01234,11111,22222,33333,44444,55555,66666,77777,88888,99999)
location <- data.frame(area)

I am able to generate all combinations from the lists via the expand.grid code but I am then looking to apply each result across the large vector of location values. What would be the best method of completing this task? Some sort of matrix?

Any help would be great! Thank you!

*####### To expand, I was requested to build a basic pricing rate table (that will sit in the background of an application). Each individual variable in the 4 vectors (year, type, update, percent) is a factor with a weight/multiplier. Each vector is in order from worst to best rating. So the combination from the 4 vectors with year of "Pre-95", type "A", update "Pre-08", and percent "1" would be the worst possible rating (I plan on adding actual values/ratings later on).

There will be 300 combinations of possible ratings and about ~30,000 locations overall. The end goal would be to apply all 300 combinations of ratings/multipliers to each of the location values in the location vector. Each location has its own individual rating, so each location multiplied by each of the 300 combinations will results in a unique result. ########

Ebc1
  • 1
  • 2
  • Make a *small* reproducible example. Maybe 3 lists with 3 items each, so the data is easily manageable and understood. Use actual R code so that it is copy/paste/runnable. [See a nice guide here if you need help setting up a reproducible example](https://stackoverflow.com/q/5963269/903061). Explain and show your desired output for that example. As it is, I have no idea what you mean by *"take calculated value of each combo and apply each to all values in Primary list below"*. Usually we apply functions, not values. – Gregor Thomas Oct 16 '17 at 18:53
  • Also, be a little more careful with technical terms. A `list` is a specific type of data structure that can hold any type of R object. It looks like you probably have `vector`s, not `list`s, which makes a difference. Also make sure you share your "primary list" reproducibly - is it a list, a data frame, a numeric vector, something else? – Gregor Thomas Oct 16 '17 at 18:56
  • @Gregor - Thanks so much for the help/tips. I just updated the post and hopefully it is now more clear/helpful. – Ebc1 Oct 17 '17 at 14:38
  • It's better, but I still don't know your goal. Your code runs, and the data structures are clear, which is good. But what output do you want. Can you give an example of your desired output for this example inout? What does "apply each modifier to the each of the location values below" mean? My best guess is that you want `expand.grid(year, type, update, percent, area)`, but it seems like you would figure that out yourself. Maybe you want to randomly assign a single combination to each area? – Gregor Thomas Oct 17 '17 at 14:43
  • @Gregor - Thanks so much again. I just added a little deeper explanation above and hopefully that is helpful. Basically each combo is a rating and each needs to be multiplied by each location (each location has its own rating). – Ebc1 Oct 17 '17 at 15:57
  • Get your modifiers as a vector, then `result = expand.grid(modifier = modifier, areas = areas)`, then `result$result = result$modifier * result$areas`. – Gregor Thomas Oct 17 '17 at 16:00

0 Answers0