There are three companies, A, B, and C. They have rates stored in vectors.
A <- c(19, 19, 19, 20, 12)
B <- c(19, 19, 20, 20, 20, 20, 19, 19, 19, 11)
C <- c(13, 13)
In this example there are 17 rates but only five unique rates. Each rate corresponds to a monthly fee over 12 months that is the same across companies.
> str(fees)
List of 5
$ 19: num [1:12] 8.5 24.8 40.9 56.6 72.1 ...
$ 20: num [1:12] 8.9 26.1 42.9 59.4 75.7 ...
$ 12: num [1:12] 5.5 16.1 26.6 37 47.2 57.4 67.4 77.3 87.1 96.8 ...
$ 11: num [1:12] 4.8 13.9 23 31.9 40.8 49.6 58.3 66.9 75.5 83.9 ...
$ 13: num [1:12] 5.9 17.4 28.6 39.8 50.8 61.7 72.4 83.1 93.6 104 ...
The goal is to build the fee matrix for each company. So for A, the fee matrix is:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 8.5 24.8 40.9 56.6 72.1 87.4 102.4 117.1 131.6 145.9 159.9 173.1
[2,] 8.5 24.8 40.9 56.6 72.1 87.4 102.4 117.1 131.6 145.9 159.9 173.1
[3,] 8.5 24.8 40.9 56.6 72.1 87.4 102.4 117.1 131.6 145.9 159.9 173.1
[4,] 8.9 26.1 42.9 59.4 75.7 91.6 107.3 122.7 137.9 152.7 167.3 181.3
[5,] 5.5 16.1 26.6 37.0 47.2 57.4 67.4 77.3 87.1 96.8 106.4 114.6
My guess is the best way to build these matrices is with a lookup table, however I am unsure as to how to do that.
R Script
A <- c(19, 19, 19, 20, 12)
B <- c(19, 19, 20, 20, 20, 20, 19, 19, 19, 11)
C <- c(13, 13)
all_comp <- c(A, B, C)
unique_comp <- unique(all_comp)
# Only 5 unique rates
# 19 20 12 11 13
fees <- list('19' = c(8.5, 24.8, 40.9, 56.6, 72.1, 87.4, 102.4, 117.1, 131.6, 145.9, 159.9, 173.1),
'20' = c(8.9, 26.1, 42.9, 59.4, 75.7, 91.6, 107.3, 122.7, 137.9, 152.7, 167.3, 181.3),
'12' = c(5.5, 16.1, 26.6, 37.0, 47.2, 57.4, 67.4, 77.3, 87.1, 96.8, 106.4, 114.6),
'11' = c(4.8, 13.9, 23.0, 31.9, 40.8, 49.6, 58.3, 66.9, 75.5, 83.9, 92.3, 99.3),
'13' = c(5.9, 17.4, 28.6, 39.8, 50.8, 61.7, 72.4, 83.1, 93.6, 104.0, 114.2, 123.1))
# Desired result
A_m <- matrix(c(rep(fees[['19']], 3), fees[['20']], fees[['12']]), 5, 12, byrow = TRUE)
CodePudding user response:
You should store the company rates in a named list and the fees in a matrix whose row names correspond to the unique rate values.
rates <- list(A = A, B = B, C = C)
lookup <- matrix(unlist(fees), ncol = length(fees[[1L]]), byrow = TRUE, dimnames = list(names(fees), NULL))
Then you can use the matrix as a lookup table as follows:
res <- lapply(rates, function(x) lookup[as.character(x), ])
The result is a named list of company fee matrices, with res[["A"]]
extracting the fee matrix for company A, and so on.
FYI, you can learn about the different ways to index vectors and arrays by reading the help page on the subset operator, accessible with ?`[`
. Here, we are indexing the rows of lookup
with a character vector whose elements are a subset of the row names of the matrix (with duplicates).
CodePudding user response:
Update:
Thanks to the comment below, the function might be as short as:
desired_result <- function(input_vec, fees) (result <- fees[as.character(input_vec)])
result <- desired_result(A, fees)
Then you can easily convert it to matrix:
result_matrix <- matrix(unlist(result),
nrow = length(result),
ncol = length(result[[1]]))
Old answer
How about wrapping it up as a separate function:
desired_result <- function(input_vec, fees) {
N <- length(input_vec)
result <- list()
for (i in 1:N) {
result[[i]] <- fees[[as.character(input_vec[i])]]
}
return(result)
}