I am working within a for loop where each iteration generates a matrix and want to store the resulting matrix within a list. The loop is structured in such a way that I cannot simply index the results list by the loop index, for example ResultsList[[i]] <- ResultsMx
will not work is this instance.
Instead, I need to add the ResultsMx
to the ResultsList
by matching names but am not sure how to index the correct names and deminsions without getting the error: number of items to replace is not a multiple of replacement length
For example, I want to add this matrix
ResultsMx <- structure(c(0.280409, 0.248557, 0.193688, 0.120225, 0.060881,
0.03218, 0.029315, 0.019525, 7e-06, 3e-06, 0.306503, 0.240209,
0.224629, 0.133996, 0.043117, 0.016981, 0.012545, 0.003268, 7.8e-05,
5.8e-05, 0.219001, 0.202018, 0.197539, 0.188427, 0.093204, 0.047265,
0.033493, 0.007712, 0.000352, 0.00015, 0.126588, 0.15627, 0.161544,
0.194544, 0.171473, 0.103416, 0.070513, 0.014678, 0.001778, 0.000774,
0.054797, 0.100531, 0.1249, 0.171576, 0.21895, 0.168987, 0.120546,
0.045224, 0.006602, 0.003019, 0.012318, 0.045509, 0.073426, 0.125358,
0.225135, 0.219425, 0.169645, 0.117345, 0.030413, 0.011488, 0.000385,
0.006894, 0.023663, 0.060295, 0.151452, 0.242802, 0.222163, 0.206509,
0.098567, 0.04849, 0, 1.2e-05, 0.000611, 0.005577, 0.035691,
0.151408, 0.239021, 0.301288, 0.230251, 0.211833, 0, 0, 0, 2e-06,
9.8e-05, 0.017526, 0.098463, 0.245786, 0.417003, 0.724185, 0,
0, 0, 0, 0, 9e-06, 0.004297, 0.038665, 0.21495, 0), .Dim = c(10L,
10L))
to this list of blank matrices:
SeasonResults <- list(BHSSummSymScReClass = matrix(0, nrow = 10, ncol = 10),
MTGSummSymScReClass = matrix(0, nrow = 10, ncol = 10),
BHSWintSymScReClass = matrix(0, nrow = 10, ncol = 10),
MTGWintSymScReClass = matrix(0, nrow = 10, ncol = 10))
by matching the list element with this object of names:
SeasonNames <- c("BHSSummSymScReClass", "MTGSummSymScReClass")
Here use the following code to match the first list item with the first SeasonName
item:
SeasonResults[names(SeasonResults) == SeasonNames[1]] <- ResultsMx
which generates the above warning and only adds the 1,1 element from the ResultsMx
.
How can I specify the correct dimensions so that the entire ResultsMx
is added to the SeasonResults
list based on a name match with SeasonNames
?
I have seen a number of related SO posts, but none that deal with my need to index based on the name match and the dimensions.
Thanks in advance.
We can use a for
loop along with the which
function. which
looks for for where names(SeasonResults)
matches those found in SeasonNames
. Then we iterate over those indices, replacing the contents with ResultsMx
.
for(i in which(names(SeasonResults) %in% SeasonNames)){
SeasonResults[[i]] <- ResultsMx
}
Just use the current name as element index
SeasonResults[[theName]] = ResultsMx;
for instance
x = list();
x[["a"]] = matrix(1:9, ncol=3);
theName = "b";
x[[theName]] = matrix(letters[1:9], ncol=3);
x;
> $a
> [,1] [,2] [,3]
> [1,] 1 4 7
> [2,] 2 5 8
> [3,] 3 6 9
>
> $b
> [,1] [,2] [,3]
> [1,] "a" "d" "g"
> [2,] "b" "e" "h"
> [3,] "c" "f" "i"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.