R - apply WGCNA pquantile() using margins of array as arguments
1
0
Entering edit mode
4.5 years ago

Hi all.

I have an array named TOM. The command dim(TOM) returns 2, 4000, 4000.

When I run:

consensusTOM <- pmin(TOM[1, , ], TOM[2, , ])

It returns a matrix containing the parallel minima values between the two input arguments, which is exactly what I need.

I want to write a function that, for whatever value I have for the first margin of this array, would apply pmin() considering each element as an individual argument to the pmin() function.

Then, if I input an array of dimensions 3, 4000, 4000, it would do:

consensusTOM <- pmin(TOM[1, , ], TOM[2, , ], TOM[3, , ])

Likewise, if I input an array of dimensions 4, 4000, 4000, it would do:

consensusTOM <- pmin(TOM[1, , ], TOM[2, , ], TOM[3, , ], TOM[4, , ])

And so on and so forth.

Does anyone know how to do this in R?

WGCNA R RNA-Seq • 839 views
ADD COMMENT
0
Entering edit mode
4.5 years ago
Alex Nesmelov ▴ 200

Hi! In case of an array with the first margin = 3, you can re-shape the data into a dataframe with 3 columns corresponding to vectors like as.vector(consensusTOM[1, , ]) etc and apply pmin function like this:

#make up the data
consensusTOM <- array(data=rnorm(3*4000*4000),
                  dim = c(3, 4000, 4000))

#re-shape
result <- matrix(consensusTOM, 
           ncol = dim(consensusTOM)[1], # number of columns = first margin
           byrow = T)

#apply pmin, transforming matrix to data frame
result = do.call(pmin, data.frame(result))

#bring back the dimensions needed
dim(result) = c(dim(consensusTOM)[2], 
          dim(consensusTOM)[3])

#verify result
all(result == pmin(consensusTOM[1, , ],
             consensusTOM[2, , ],
             consensusTOM[3, , ]))

It will work with any first margin value. I suggest using do.call instead of apply() since apply on such large matrixes can take minutes to execute. There are also other ways to reshape using tidyverse etc, but they will be considerably slower than approach above.

ADD COMMENT

Login before adding your answer.

Traffic: 1835 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6