Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R get()函数错误_R - Fatal编程技术网

R get()函数错误

R get()函数错误,r,R,我正在尝试填充一组 矩阵(对象)名称保存在列表中的矩阵。我可以 使用get()返回具有给定名称的对象,但我 使用get()定义矩阵时遇到函数问题 我正在尝试填充的对象 #Create list of matrix names: list.names <- c("m.1") #Create matrix object m.1 <- matrix(nrow=2,ncol=2) #Return matrix m.1 from the list.names using get(). Wo

我正在尝试填充一组 矩阵(对象)名称保存在列表中的矩阵。我可以 使用get()返回具有给定名称的对象,但我 使用get()定义矩阵时遇到函数问题 我正在尝试填充的对象

#Create list of matrix names:
list.names <- c("m.1")

#Create matrix object
m.1 <- matrix(nrow=2,ncol=2)

#Return matrix m.1 from the list.names using get(). Works great!
get(list.names[1])

#Populate m.1 with some values. Doesn't work.
get(list.names[1]) <- c(1,2,3,4)
但当我尝试填充矩阵时,R返回类似的“找不到函数”错误

有人能解释一下我的方法中的错误吗

编辑/更新:

因此,在我试图简化发布在这里的问题时,我意识到我可能把我试图做的事情简单化了

所以,事实上,我试图在一组矩阵中填充元素。矩阵的名称包含在list.names对象中。我使用嵌套的for()循环来填充矩阵中的每个元素

因此,事实上,我的问题可以更准确地表述为:

get(list.names[1])[1,1] <- some_value

get(list.names[1])[1,1]使用函数
assign
而不是
get

assign(list.names[1],c(1,2,3,4))
get
返回对象的值,
assign
assigns.)


eval
相同,它只是评估您的呼叫。

这在常见问题解答7.21中有介绍。FAQ最重要的部分是它的结尾,它说要使用一个列表(一个真实的列表,而不是你在上面称之为列表的向量)。如果你的全球工作空间中有一个矩阵列表,而不是一堆矩阵,那么很多事情就会变得容易得多。以下是一个例子:

mnames <- c('m.1','m.2','m.3')
m.1 <- matrix(1, 2, 2)
m.2 <- matrix(2, 2, 2)
m.3 <- matrix(3, 2, 2)

## put matricies into a list
mymats <- lapply( mnames, get )
names(mymats) <- mnames

## change 1 value in each matrix a different way
mymats[['m.2']][1,1] <- 22
mymats[[1]][2,2] <- 11
tmp <- "m.3"
mymats[[tmp]][1,2] <- 33

## change the same element in each matrix using a loop
for( i in seq_along(mymats) ) {
 mymats[[i]][2,1] <- 44
}

## apply the same function to every matrix and simplify the output
sapply( mymats, rowMeans )

mnames我想你想要
assign
就像
assign(get(list.names[1]),1:4)
一样。我无法解释具体细节,但解释可能听起来像是:从
get
返回的对象没有赋值(
@Justin不太有。是
get()
没有赋值函数,而不是
get()返回的对象)
。本质上没有
的gethanks everyone。可以使用assign来更改/填充对象中的单个元素吗?实际上,我的代码是一系列嵌套循环,在这些循环中,我使用辅助函数生成元素值。我尝试了:assign(list.names[1][1,1],1)如果运气不好,您可能希望使用
中的
查看
,…
assign
创建具有名称的对象,而
中的
允许访问内部组件。为什么不更准确地描述您的问题?
mnames <- c('m.1','m.2','m.3')
m.1 <- matrix(1, 2, 2)
m.2 <- matrix(2, 2, 2)
m.3 <- matrix(3, 2, 2)

## put matricies into a list
mymats <- lapply( mnames, get )
names(mymats) <- mnames

## change 1 value in each matrix a different way
mymats[['m.2']][1,1] <- 22
mymats[[1]][2,2] <- 11
tmp <- "m.3"
mymats[[tmp]][1,2] <- 33

## change the same element in each matrix using a loop
for( i in seq_along(mymats) ) {
 mymats[[i]][2,1] <- 44
}

## apply the same function to every matrix and simplify the output
sapply( mymats, rowMeans )