Reputation: 5486
This is my problem:
There is a predefined list named gamma
with three entries: gamma$'2'
is 2x2 matrix gamma$'3'
a 3x3 matrix and gamma$'4'
a 4x4 matrix. I would like to have function that returns the matrix I need:
GiveMatrix <- function(n) {
gamma.list <- #init the list of matrices
gamma.list$n # return the list entry named n
Since n
is not a character, the last line does not work. I tried gamma.list$paste(n)
and gamma.list$as.character(n)
but both did not work. Is there a function that converts n
to the right format? Or is there maybe a much better way? I know, I am not really good in R.
Upvotes: 0
Views: 4957
Reputation: 5486
I've found it!
gamma.list[as.character(n)]
is the solution I needed.
Upvotes: 0
Reputation: 60984
You need to use:
gamma.list[[as.character(n)]]
In your example, R is looking for a entry in the list called n
. When using [[
, the contents of n
is used, which is what you need.
Upvotes: 4