循环在向量中的元素,元素是矩阵

时间:2017-05-28 04:36:21

标签: r loops vectorization

我今天开始使用R,所以如果这太基础我就道歉。

首先,我构造2个矩阵,并构造一个向量,其条目是这些矩阵。然后,我尝试遍历向量的元素,即矩阵。但是,当我这样做时,我会得到一个长度为零的"参数。错误。

cam <- 1:12
ped <- 13:24

dim(cam) <- c(3,4)
dim(ped) <- c(4,3)

mats <- c('cam','ped')

for (i in  1:2) {
  rownames(mats[i]) <- LETTERS[1:dim(mats[i])[1]]
  colnames(mats[i]) <- LETTERS[1:dim(mats[i])[2]]
}

错误文本如下:

Error in 1:dim(mats[i])[1] : argument of length 0

问题:如何循环向量的元素,这些元素是矩阵? (我猜测我没有正确地调用这些元素)。谢谢你的耐心等待。

2 个答案:

答案 0 :(得分:2)

R中的首选项是使用列表:

cam <- 1:12 ; dim(cam) <- c(3,4)
# same as matrix(1:12, nrow = 3, ncol = 4)
ped <- 13:24 ; dim(ped) <- c(4,3)

# save the list ( '=' sign for naming purposes only here)
mats <- list(cam = cam, ped = ped)

# notice the double brackets '[[' which is used for picking the list
for (i in  1:length(mats) {
  rownames(mats[[i]]) <- LETTERS[1:dim(mats[[i]])[1]]
  colnames(mats[[i]]) <- LETTERS[1:dim(mats[[i]])[2]]
}

# finally you can call the whole list at once as follows:
mats
# or seperately using $ or [[
mats$cam # mats[['cam']]
mats$ped # mats[['ped']]

<强>替代地

如果你真的想发疯,可以利用get()assign()功能。 get()按字符调用对象,assign()可以创建一个。

mats <- c('cam','ped')
mats.new <- NULL # initialize a matrix placeholder
for (i in  1:length(mats)) {
  mats.new <- get(mats[i]) # save as a new matrix each loop
  # use dimnames with a list input to do both the row and column at once
  dimnames(mats.new) <- list(LETTERS[1:dim(mats.new)[1]],
                             LETTERS[1:dim(mats.new)[2]]) 
  assign(mats[i],mats.new) # create (re-write) the matrix
}

答案 1 :(得分:1)

如果数据集放在list中,我们可以使用lapply

lst <- lapply(mget(mats), function(x) {
         dimnames(x) <-  list(LETTERS[seq_len(nrow(x))], LETTERS[seq_len(ncol(x))])
    x})

最好将其保存在list中。如果需要更改原始对象

list2env(lst, envir = .GlobalEnv)