我想用适用于跨矩阵的行运行,我想用当前行的rowname在我的功能。 看来你不能使用rownames
, colnames
, dimnames
或names
直接在函数内部。 我知道我可以基于信息可能创建一个解决方法这个问题 。
但我的问题是如何apply
在阵列中它的第一个参数,和名称由调用的函数内部创建的对象分配的手柄行和列名apply
? 这似乎有点矛盾,因为我希望通过下面的例子来说明。 有它为什么这样设计的理由吗?
# Toy data
m <- matrix( runif(9) , nrow = 3 )
rownames(m) <- LETTERS[1:3]
colnames(m) <- letters[1:3]
m
a b c
A 0.5092062 0.3786139 0.120436569
B 0.7563015 0.7127949 0.003358308
C 0.8794197 0.3059068 0.985197273
# These return NULL
apply( m , 1 , FUN = function(x){ rownames(x) } )
NULL
apply( m , 1 , FUN = function(x){ colnames(x) } )
NULL
apply( m , 1 , FUN = function(x){ dimnames(x) } )
NULL
# But...
apply( m , 1 , FUN = function(x){ names(x) } )
A B C
[1,] "a" "a" "a"
[2,] "b" "b" "b"
[3,] "c" "c" "c"
# This looks like a column-wise matrix of colnames, with the rownames of m as the column names to me
# And further you can get...
n <- apply( m , 1 , FUN = function(x){ names(x) } )
dimnames(n)
[[1]]
NULL
[[2]]
[1] "A" "B" "C"
# But you can't do...
apply( m , 1 , FUN = function(x){ n <- names(x); dimnames(n) } )
NULL
我只是想了解内部情况的申请? 非常感谢。