Frage

Suppose I have an array such that:

temp<-array(0, dim=c(100,10,4))

I can merge matrices 1 & 2 from the array into a single matrix using cbind:

temp.merge<-cbind(temp[,,1],temp[,,2])

Is there a way to merge all n matrices (in this case 4) into a single one without having to write out the position of each one as above?

War es hilfreich?

Lösung

If you have the array set up right in memory, you can just reset the dimensions and it will work.

dim(temp) <- c(100, 40)

Andere Tipps

If @Neal's answer works, definately use it.

This also works:

# generates 100 X 40 array
do.call("cbind",lapply(1:4,function(x){return(temp[,,x])}))

You would think that:

do.call("cbind",list(temp[,,1:4]))    ## generates 4000 X 1 array

would work, but it does not...

Also:

as.matrix(as.data.frame(temp))

Example:

> temp <- array(1:8, dim=c(2,2,2))
> temp
#, , 1
# 
#     [,1] [,2]
#[1,]    1    3
#[2,]    2    4
#
#, , 2
# 
#     [,1] [,2]
#[1,]    5    7
#[2,]    6    8


as.matrix(as.data.frame(temp))

#     V1 V2 V3 V4
#[1,]  1  3  5  7
#[2,]  2  4  6  8
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top