开发者

R - indirectly calling a matrix using a string

Example: There is a matrix of data called VE There 开发者_如何转开发is a vector of string where the first element is the string VE. I need to indirectly call the string and be able to access data. For example if I need the 6th column of matrix VE then I want to do:

Vector[1][,6]

Essentially I need R to start reading those string as if they are the matrix names that are already in this page. I need this syntax to be dynamic because I am putting it in a loop.


I think you are looking for get():

VE <- matrix(0,10,10)
vec <- c("VE","foo","bar")
get(vec[1])[,6]

Edit:

This requires a global object called VE though, for automating it is probably better to keep the matrices (I assume there are more?) in a list, then you can just index:

matrixlist <- list(VE = matrix(0,10,10))
vec <- c("VE","foo","bar")
matrixlist[[vec[1]]][,6]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜