R:应用中的行名、列名、暗号和名称 [英] R: rownames, colnames, dimnames and names in apply

查看:22
本文介绍了R:应用中的行名、列名、暗号和名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 apply 来跨越矩阵的行,并且我想在我的函数中使用当前行的行名.似乎您不能直接在函数内部使用 rownamescolnamesdimnamesnames.我知道我可以根据这个问题中的信息创建一个解决方法.

I would like to use apply to run across the rows of a matrix, and I would like to use the rowname of the current row in my function. It seems you can't use rownames, colnames, dimnames or names directly inside the function. I am aware that I can probably create a workaround based on information in this question.

但我的问题是 apply 如何处理数组的第一个参数中的行名和列名,以及如何将名称分配给 apply?这似乎有点不一致,我希望通过以下示例来说明.设计成这样有什么原因吗?

But my question is how does apply handle row and column names of the array in it's first argument, and the assignment of names to objects created inside the function called by apply? It seems a bit inconsistent, as I hope to show by the following example. Is there a reason why it was designed like this?

# 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

我只想了解 apply 内部会发生什么?非常感谢.

I just want to understand what happens internally in apply? Many thanks.

推荐答案

我认为您的困惑源于 apply 没有将数组(或矩阵)传递给 有趣.

I think your confusion stems from the fact that apply does not pass an array (or matrix) to the function specified in FUN.

它依次传递矩阵的每一行.每行本身仅"是一个(命名的)向量:

It passes each row of the matrix in turn. Each row is itself "only" a (named) vector:

> m[1,]
         a          b          c 
0.48768161 0.61447934 0.08718875 

所以你的函数只有这个命名向量可以使用.

So your function has only this named vector to work with.

对于您的中间示例,如 apply 中所述:

For your middle example, as documented in apply:

如果每次调用 FUN 都返回一个长度为 n 的向量,则 apply 返回一个维度为 c(n, dim(X)[MARGIN]) 的数组,如果 n > 1.如果 n 等于 1,如果 MARGIN 的长度为 1 并且是一个数组,则 apply 返回一个向量维度 dim(X)[MARGIN] 否则.

If each call to FUN returns a vector of length n, then apply returns an array of dimension c(n, dim(X)[MARGIN]) if n > 1. If n equals 1, apply returns a vector if MARGIN has length 1 and an array of dimension dim(X)[MARGIN] otherwise.

所以 function(x) names(x) 为每一行返回一个长度为 3 的向量,所以最终结果就是你看到的矩阵.但是该矩阵是在 apply 函数的末尾构建的,基于 FUN 单独应用于每一行的结果.

So function(x) names(x) returns a vector of length 3 for each row, so the final result is the matrix you see. But that matrix is being constructed at the end of the apply function, on the results of FUN being applied to each row individually.

这篇关于R:应用中的行名、列名、暗号和名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆