Domanda

I'm using aaply (of the plyr package) to provide simple summary stats on an n-space array. The source array has axis headers but not row/column names. A simplified example:

mtx <- array(1:24, dim=c(3,4,2))
dimnames(mtx) <- list(A=NULL, B=NULL, C=NULL)
mtx
## , , 1
## 
##       B
## A      [,1] [,2] [,3] [,4]
##   [1,]    1    4    7   10
##   [2,]    2    5    8   11
##   [3,]    3    6    9   12
## 
## , , 2
## 
##       B
## A      [,1] [,2] [,3] [,4]
##   [1,]   13   16   19   22
##   [2,]   14   17   20   23
##   [3,]   15   18   21   24

Note that I have axis names but no row/column names. When I want to summarize along an axis, header names are added:

(mtx2 <- aaply(mtx, c(3,2), sum))
##    B
## C    1  2  3  4
##   1  6 15 24 33
##   2 42 51 60 69

Note that we now have row/column names (strings) of '1', '2', etc. I can manually remove them with:

dimnames(mtx2) <- list(C=NULL, B=NULL)

or more generically with:

dimnames(mtx2) <- structure(replicate(length(dim(mtx2)), NULL), .Names=names(dimnames(mtx2)))

but I would prefer to not include the row/column headers in the first place.

Is there an option I'm missing (to aaply) to do this?

(BTW: I know that it's mostly cosmetic, and that I can still reference all rows/columns/etc using the integer indices. My point is mostly aesthetic but is focused on providing a consistent output from function calls.)

Edit: I'm using some other options that aaply() provides that apply() does not, so I was hoping to be consistent with the function calls.

È stato utile?

Soluzione

So just to be clear, you are using aaply(...) in the plyr package, and it is adding row and column names:

library(plyr)
mtx2 <- aaply(mtx, c(3,2), sum)
str(mtx2)
#  int [1:2, 1:4] 6 42 15 51 24 60 33 69
#  - attr(*, "dimnames")=List of 2
#   ..$ C: chr [1:2] "1" "2"
#   ..$ B: chr [1:4] "1" "2" "3" "4"

You can achieve the same result in base R using apply(...), without getting column or row names:

mtx3 <- apply(mtx, c(3,2), sum)
all(mtx2 == mtx3)
# [1] TRUE
str(mtx3)
#  int [1:2, 1:4] 6 42 15 51 24 60 33 69
#  - attr(*, "dimnames")=List of 2
#   ..$ C: NULL
#   ..$ B: NULL
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top