[R] Create matrix with subset from unlist

David Winsemius dwinsemius at comcast.net
Mon Feb 1 16:49:16 CET 2010


On Feb 1, 2010, at 9:38 AM, Muhammad Rahiz wrote:

> Hello all,
>
> Thanks for all your replies.
>
> Usually, when I make a post to the R-mailing list, I would keep on  
> trying to get the solution myself rather than waiting for one. At  
> times, I was able to derive my own solution. This would explain why  
> my solution and that of Dennis's produces the same result - not that  
> I totally ignore the method of his...
>
> Anyway, I did manage to create the matrix I require. But there is  
> still the recurring problem of " (list) object cannot be coerced to  
> type 'double'  " in further analysis using the dataset. I thought I   
> could resolve it by changing converting to matrix. Seems not.
>
> Given, the following, is there any other way I can define y other  
> that using list()? Seems that producing a list of matrices does not  
> work.

I produced an array as an intermediate result in an earlier posting.  
If you defined the dimensions properly that would seem to be a  
sensible alternative.

 > dput(x)
list(structure(list(V1 = c(-27.3, 29), V2 = c(14.4, -38.1)), .Names =  
c("V1",
"V2"), class = "data.frame", row.names = c("1", "2")), structure(list(
    V1 = c(14.4, -38.1), V2 = c(29, -3.4)), .Names = c("V1",
"V2"), class = "data.frame", row.names = c("1", "2")), structure(list(
    V1 = c(29, -3.4), V2 = c(-38.1, 55.1)), .Names = c("V1",
"V2"), class = "data.frame", row.names = c("1", "2")))

 > xx <- array( , dim=c(2,2,3))
 > xx[,,1:3] <- sapply(x, data.matrix)

All you would need to do is change the 3's to 32.



>
>
> y <-
> for (i in 1:32){
> y[[i]] <- matrix(xx[c(1:4)],2,2)
> }
>
>
> Muhammad
>
>
>
>
> Dennis Murphy wrote:
>> Correct me if I'm wrong, but isn't this the solution I gave??
>>
>> On Fri, Jan 29, 2010 at 9:43 AM, Muhammad Rahiz <muhammad.rahiz at ouce.ox.ac.uk 
>>  <mailto:muhammad.rahiz at ouce.ox.ac.uk>> wrote:
>>
>>    Thanks David & Dennis,
>>
>>    I may have found something.
>>
>>    Given that the object xx is the product of unlist(x), to create a
>>    2x2 matrix with subsets, I could do,
>>
>>    > y <- matrix(xx[c(1:4)], 2, 2).
>>
>> First object named y...
>>
>>    This returns,
>>
>>
>>        [,1]  [,2]
>>    [1,] -27.3  14.4
>>    [2,]  29.0 -38.1
>>
>>    If I do,
>>
>>    > y2 <- matrix(xx[c(5:8)],2,2)
>>
>>
>> second object named y2
>>
>>    it returns,
>>
>>
>>        [,1] [,2]
>>    [1,]  14.4 29.0
>>    [2,] -38.1 -3.4
>>
>>
>> And I presume you want to do the same with the remaining 30 matrices,
>> assigning them to different objects. That is *precisely* what my  
>> solution
>> provided. Run it, observe the results and tell me what it is that  
>> differs from
>> what you want, because I don't see it.
>>
>> Dennis
>>
>>    The results are exactly what I want to achieve.
>>
>>    The question is, how can I incorporate the increment in a for loop
>>    so that it becomes
>>
>>    c(1:4)
>>    c(5:8)
>>    c(9:12) and so on
>>
>>    How should I modify this code?
>>
>>    y <-            # typeof ? for (i in 1:32){
>>    y[[i]] <- matrix(xx[c(1:4)],2,2)
>>    }
>>
>>
>>    Muhammad
>>
>>
>>    David Winsemius wrote:
>>
>>        On Jan 29, 2010, at 9:45 AM, Dennis Murphy wrote:
>>
>>
>>            Hi:
>>
>>            The problem, I'm guessing, is that you need to assign each
>>            of the  matrices
>>            to an object.
>>            There's undoubtedly a slick apply family solution for this
>>            (which I  want to
>>            see, BTW!),
>>
>>
>>        I don't have a method that would assign names but you could
>>        populate  an array of sufficient size and dimension. I
>>        populated a three-element  list with his data:
>>
>>         > dput(x)
>>        list(structure(list(V1 = c(-27.3, 29), V2 = c(14.4, -38.1)),
>>        .Names =  c("V1",
>>        "V2"), class = "data.frame", row.names = c("1", "2")),
>>        structure(list(
>>            V1 = c(14.4, -38.1), V2 = c(29, -3.4)), .Names = c("V1",
>>        "V2"), class = "data.frame", row.names = c("1", "2")),
>>        structure(list(
>>            V1 = c(29, -3.4), V2 = c(-38.1, 55.1)), .Names = c("V1",
>>        "V2"), class = "data.frame", row.names = c("1", "2")))
>>
>>         > xx <- array( , dim=c(2,2,3))
>>
>>         > xx[,,1:3] <- sapply(x, data.matrix)
>>         > xx
>>        , , 1
>>
>>              [,1]  [,2]
>>        [1,] -27.3  14.4
>>        [2,]  29.0 -38.1
>>
>>        , , 2
>>
>>              [,1] [,2]
>>        [1,]  14.4 29.0
>>        [2,] -38.1 -3.4
>>
>>        , , 3
>>
>>             [,1]  [,2]
>>        [1,] 29.0 -38.1
>>        [2,] -3.4  55.1
>>
>>        Without the more complex structure ready to accept the 2x2
>>        arrays I  got this:
>>
>>         > sapply(x, data.matrix)
>>              [,1]  [,2]  [,3]
>>        [1,] -27.3  14.4  29.0
>>        [2,]  29.0 -38.1  -3.4
>>        [3,]  14.4  29.0 -38.1
>>        [4,] -38.1  -3.4  55.1
>>
>>
>>

David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list