[R] Proper apply function for matrices embedded in lists

Uwe Ligges ligges at statistik.tu-dortmund.de
Thu Apr 26 14:30:36 CEST 2012



On 26.04.2012 01:18, Bcampbell99 wrote:
> Hi:
>
> I have a small problem that I'm not quite sure how to figure out.  I've
> generated some randomized (permutated) data which consists of nrows=3 by
> ncols=165 (see below) per element (3 x 165 = 495 sub-elements).
>
>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
> [,14].......
> [1,]    0    0    0    0    0    0    0    0    0     0     0     0     0
> 0
> [2,]    0    0    0    0    0    0    0    0    0     0     0     1     0
> 0
> [3,]    0    0    0    0    0    0    0    0    0     0     0     1     0
> 1
>
>       [,159] [,160] [,161] [,162] [,163] [,164] [,165]
> [1,]      0      0      0      1      0      0      9
> [2,]      0      0      0      0      0      0      8
> [3,]      0      0      0      2      0      0      9
>
> I generated 1000 permutations of this structure which was then stored in a
> list-like object (still actually called a matrix)
>
>        [,1]        [,2]        [,3]        [,4]        [,5]        [,6]
> .....[,1000]
>
>   [1,] Numeric,495 Numeric,495 Numeric,495 Numeric,495 Numeric,495
> Numeric,495
>   [2,] Numeric,495 Numeric,495 Numeric,495 Numeric,495 Numeric,495
> Numeric,495
>   [3,] Numeric,495 Numeric,495 Numeric,495 Numeric,495 Numeric,495
> Numeric,495
>   [4,] Numeric,495 Numeric,495 Numeric,495 Numeric,495 Numeric,495
> Numeric,495
>   [5,] Numeric,495 Numeric,495 Numeric,495 Numeric,495 Numeric,495
> Numeric,495
>   [6,] Numeric,495 Numeric,495 Numeric,495 Numeric,495 Numeric,495
> Numeric,495
>   [7,] Numeric,495 Numeric,495 Numeric,495 Numeric,495 Numeric,495
> Numeric,495
>   [8,] Numeric,495 Numeric,495 Numeric,495 Numeric,495 Numeric,495
> Numeric,495
>   [9,] Numeric,495 Numeric,495 Numeric,495 Numeric,495 Numeric,495
> Numeric,495
>
> Where [1,]...[2,]....[9,] represent sites (sampling units) and
> [,1],[,2].....[,1000] represent permutations.
>
> I'd like to take the column sums of from each matrix so instead of having a
> r (3) x c (165) structure, I would get r(1) x (165) structure while
> maintaining the [1,].....[,1000] design.
>
> When I tried
>
> apply(data[1,1],2,sum) for example, I got an error;


data[1,1] is a list of length 1, hence  data[1,1][[1]] will do.

But easier:

d <- dim(data)
res <- sapply(data, function(x) list(colSums(x)))
dim(res) <- d

or perhaps as an array:

d <- dim(data)
res <- sapply(data, colSums)
dim(res) <- c(2, d)
res

Uwe Ligges






>   in apply(data[1,1], 2, sum) :
>    dim(X) must have a positive length
>
>
> Anyone have a quick suggestion (I'm sure there is a simple solution out
> there) how to do this (have I provided enough background information on the
> problem?).
>
> Most appreciatively:
>
>
> Brian Campbell
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Proper-apply-function-for-matrices-embedded-in-lists-tp4588386p4588386.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list