[R] Merging data in arrays
Jeff Newmiller
jdnewmil at dcn.davis.CA.us
Sun Feb 24 08:56:24 CET 2013
There is a reason why merge is not designed to work directly with arrays: the matching of rows can easily yield a result with a different number of rows than the initial data had. This plays havoc with the nature of arrays. I would recommend that if a merge is really what you want then unrolling your original data into data frames and leaving them that way as much as possible will be a wise move to make sooner rather than later.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
Rui Barradas <ruipbarradas at sapo.pt> wrote:
>Hello,
>
>I bet there are simpler solutions but I'm not thinking of anything
>else,
>right now.
>
>fun <- function(x, y){
> f <- function(a, b){
> a <- as.data.frame(a)
> b <- as.data.frame(b)
> names(a)[1] <- names(b)[1] <- "V1"
> res <- merge(b, a, by = "V1")
> if(nrow(res) > 0) as.matrix(res) else NULL
> }
> dx <- dim(x)[3]
> dy <- dim(y)[3]
> res <- list()
> ires <- 0
> for(idx in seq_len(dx)){
> for(idy in seq_len(dy)){
> tmp <- f(x[, , idx], y[, , idy])
> if(!is.null(tmp)){
> ires <- ires + 1
> res[[ires]] <- tmp
> }
> }
> }
> k <- nrow(res[[1]])
> m <- ncol(res[[1]])
> n <- length(res)
> array(unlist(res), dim = c(k, m, n))
>}
>
>G <- fun(C, E)
>identical(F, G) #TRUE
>
>
>
>Hope this helps,
>
>Rui Barradas
>
>Em 22-02-2013 03:40, Ray Cheung escreveu:
>> Thanks, Jeff.
>>
>> Here is a simplified hypothetical sample (sorry for the clumsy code):
>> A1 <- matrix(1:5, nrow=5, ncol=1)
>> A2 <- matrix(6:10, nrow=5, ncol=1)
>> A3 <- matrix(11:15, nrow=5, ncol=1)
>> A4 <- matrix(16:20, nrow=5, ncol=1)
>> A5 <- matrix(21:25, nrow=5, ncol=1)
>> A6 <- matrix(26:30, nrow=5, ncol=1)
>> B1 <- matrix(c(A1, A2, A3), nrow=5, ncol=3)
>> B2 <- matrix(c(A2, A3, A4), nrow=5, ncol=3)
>> B3 <- matrix(c(A3, A4, A5), nrow=5, ncol=3)
>> C <- array(c(B1, B2, B3), dim = c(5,3,3))
>> D1 <- matrix(c(A1, A4, A5), nrow=5, ncol=3)
>> D2 <- matrix(c(A3, A5, A6), nrow=5, ncol=3)
>> E <- array(c(D1, D2), dim = c(5,3,2))
>> In the above example, I want to merge array C to array E by matching
>the
>> column 1. That is, the resultant array F should look like this:
>> F1 <- matrix(c(A1, A4, A5, A2, A3), nrow=5, ncol=5)
>> F2 <- matrix(c(A3, A5, A6, A4, A5), nrow=5, ncol=5)
>> F <- array(c(F1, F2), dim = c(5,5,2))
>>
>> I want to have a more general way to do the merging because in
>reality, the
>> dimensions of C and E are very over thousands. Thank you very much.
>>
>> Best Regards,
>> ray
>>
>>
>> On Fri, Feb 22, 2013 at 11:28 AM, Jeff Newmiller
>> <jdnewmil at dcn.davis.ca.us>wrote:
>>
>>> I think this specification is insufficient to respond accurately to.
>>> Please make a reproducible subset of your data (or simulated data)
>and
>>> provide it in dput form, and describe your desired result data set
>more
>>> clearly.
>>>
>>>
>>>
>http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
>>>
>>>
>---------------------------------------------------------------------------
>>> Jeff Newmiller The ..... ..... Go
>Live...
>>> DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#.
>Live
>>> Go...
>>> Live: OO#.. Dead: OO#..
>Playing
>>> Research Engineer (Solar/Batteries O.O#. #.O#.
>with
>>> /Software/Embedded Controllers) .OO#. .OO#.
>rocks...1k
>>>
>---------------------------------------------------------------------------
>>> Sent from my phone. Please excuse my brevity.
>>>
>>> Ray Cheung <ray1728 at gmail.com> wrote:
>>>
>>>> Dear All,
>>>>
>>>> I've 2 arrays A and B:
>>>>
>>>> dim(A) = 100, 10, 1000
>>>> dim(B) = 100, 20, 900
>>>>
>>>> I know there are 5 columns of values common to both arrays. I want
>to
>>>> ask
>>>> how to merge the 2 arrays. Thanks in advance!
>>>>
>>>> Best Regards,
>>>> Ray
>>>>
>>>> [[alternative HTML version deleted]]
>>>>
>>>> ______________________________________________
>>>> 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.
>>>
>>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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