我正在寻找使用R的rownames()函数将行名分配给矩阵的方法。我有一个字符串,其中包含矩阵的名称,要分配的行名称的列表,并通过get()函数获取矩阵名称的值。
仅在get()函数的输出上调用rownames()会引发错误,但是如果我将get()的输出分配给变量,则使用rownames()即可。
mat = "matrix1" # the name of my matrix variable, as a string
components = c("1", "2", "3") # my row names
temp = get(mat) # temp now holds the value of matrix1
# This fails
> rownames(get(mat)) = components
Error in rownames(get(mat)) = components :
could not find function "get<-"
# But this works
> rownames(temp) = components
>
我知道这是一个简单的解决方法,只需将get()输出分配给变量,我对R还是陌生的,并且对为什么嵌套函数调用无效的原因非常感兴趣。
我希望R只需将带有新行名的矩阵输出到终端,然后终止。