Hello all,
I'm trying to create a 2x2 matrix, 32 times after unlist() so that I can
convert the list to matrix. I've looked through the R archive but
couldn't find the answer. There is what I've done.
> f <- system("ls *.txt", intern=TRUE)
> x <- lapply(f, read.table)
> x
[[1]]
V1 V2
1 -27.3 14.4
2 29.0 -38.1
[[2]]
V1 V2
1 14.4 29.0
2 -38.1 -3.4
[[3]]
V1 V2
1 29.0 -38.1
2 -3.4 55.1
[[4]]
V1 V2
1 -38.1 -3.4
2 55.1 -1.0
[[5]]
V1 V2
1 -3.4 55.1
2 -1.0 21.9
[[6]]
V1 V2
1 55.1 -1.0
2 21.9 -10.9
...
> xx <- unlist(x)
V11 V12 V21 V22 V11 V12 V21 V22 V11 V12 V21 V22
-27.3 29.0 14.4 -38.1 14.4 -38.1 29.0 -3.4 29.0 -3.4 -38.1 55.1
V11 V12 V21 V22 V11 V12 V21 V22 V11 V12 V21 V22
-38.1 55.1 -3.4 -1.0 -3.4 -1.0 55.1 21.9 55.1 21.9 -1.0 -10.9
V11 V12 V21 V22 V11 V12 V21 V22 V11 V12 V21 V22
-1.0 -10.9 21.9 -7.8 21.9 -7.8 -10.9 -48.2 -10.9 -48.2 -7.8 -44.9
V11 V12 V21 V22 V11 V12 V21 V22 V11 V12 V21 V22
-7.8 -44.9 -48.2 -43.8 -48.2 -43.8 -44.9 -10.3 -44.9 -10.3 -43.8 44.2
V11 V12 V21 V22 V11 V12 V21 V22 V11 V12 V21 V22
-43.8 44.2 -10.3 -0.5 -10.3 -0.5 44.2 96.7 44.2 96.7 -0.5 -32.0
V11 V12 V21 V22 V11 V12 V21 V22 V11 V12 V21 V22
-0.5 -32.0 96.7 -0.2 96.7 -0.2 -32.0 -38.6 -32.0 -38.6 -0.2 73.6
V11 V12 V21 V22 V11 V12 V21 V22 V11 V12 V21 V22
-0.2 73.6 -38.6 -17.5 -38.6 -17.5 73.6 -57.8 73.6 -57.8 -17.5 10.7
V11 V12 V21 V22 V11 V12 V21 V22 V11 V12 V21 V22
-17.5 10.7 -57.8 -33.4 -57.8 -33.4 10.7 46.1 10.7 46.1 -33.4 26.7
V11 V12 V21 V22 V11 V12 V21 V22 V11 V12 V21 V22
-33.4 26.7 46.1 -37.3 46.1 -37.3 26.7 1.2 26.7 1.2 -37.3 36.3
V11 V12 V21 V22 V11 V12 V21 V22 V11 V12 V21 V22
-37.3 36.3 1.2 39.6 1.2 39.6 36.3 31.0 36.3 -27.3 39.6 14.4
V11 V12 V21 V22 V11 V12 V21 V22
39.6 29.0 31.0 -38.1 31.0 -3.4 -27.3 55.1
The output should be
[[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
...
Thanks and much appreciated!
Muhammad
Create matrix with subset from unlist
7 messages · Dennis Murphy, David Winsemius, Muhammad Rahiz
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100129/c6f24a14/attachment.pl>
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.
> but here's the brute force method using a loop:
>
> nms <- paste('x', 1:32, sep = "")
> for(i in seq_along(nms)) assign(nms[i], x[[i]])
>
> HTH,
> Dennis
>
> On Fri, Jan 29, 2010 at 6:30 AM, Muhammad Rahiz <
> muhammad.rahiz at ouce.ox.ac.uk> wrote:
>
>> Hello all,
>>
>> I'm trying to create a 2x2 matrix, 32 times after unlist() so that
>> I can
>> convert the list to matrix. I've looked through the R archive but
>> couldn't
>> find the answer. There is what I've done.
>>
>>
>>> f <- system("ls *.txt", intern=TRUE)
>>> x <- lapply(f, read.table)
>>> x
>> [[1]]
>> V1 V2
>> 1 -27.3 14.4
>> 2 29.0 -38.1
>>
>> [[2]]
>> V1 V2
>> 1 14.4 29.0
>> 2 -38.1 -3.4
>>
>> [[3]]
>> V1 V2
>> 1 29.0 -38.1
>> 2 -3.4 55.1
>>
>> [[4]]
>> V1 V2
>> 1 -38.1 -3.4
>> 2 55.1 -1.0
>>
>> [[5]]
>> V1 V2
>> 1 -3.4 55.1
>> 2 -1.0 21.9
>>
>> [[6]]
>> V1 V2
>> 1 55.1 -1.0
>> 2 21.9 -10.9
>>
>> ...
>>
>>> xx <- unlist(x)
>> V11 V12 V21 V22 V11 V12 V21 V22 V11 V12 V21 V22
>> -27.3 29.0 14.4 -38.1 14.4 -38.1 29.0 -3.4 29.0 -3.4 -38.1
>> 55.1
>> V11 V12 V21 V22 V11 V12 V21 V22 V11 V12 V21 V22
>> -38.1 55.1 -3.4 -1.0 -3.4 -1.0 55.1 21.9 55.1 21.9 -1.0
>> -10.9
>> V11 V12 V21 V22 V11 V12 V21 V22 V11 V12 V21 V22
>> -1.0 -10.9 21.9 -7.8 21.9 -7.8 -10.9 -48.2 -10.9 -48.2 -7.8
>> -44.9
>> V11 V12 V21 V22 V11 V12 V21 V22 V11 V12 V21 V22
>> -7.8 -44.9 -48.2 -43.8 -48.2 -43.8 -44.9 -10.3 -44.9 -10.3 -43.8
>> 44.2
>> V11 V12 V21 V22 V11 V12 V21 V22 V11 V12 V21 V22
>> -43.8 44.2 -10.3 -0.5 -10.3 -0.5 44.2 96.7 44.2 96.7 -0.5
>> -32.0
>> V11 V12 V21 V22 V11 V12 V21 V22 V11 V12 V21 V22
>> -0.5 -32.0 96.7 -0.2 96.7 -0.2 -32.0 -38.6 -32.0 -38.6 -0.2
>> 73.6
>> V11 V12 V21 V22 V11 V12 V21 V22 V11 V12 V21 V22
>> -0.2 73.6 -38.6 -17.5 -38.6 -17.5 73.6 -57.8 73.6 -57.8 -17.5
>> 10.7
>> V11 V12 V21 V22 V11 V12 V21 V22 V11 V12 V21 V22
>> -17.5 10.7 -57.8 -33.4 -57.8 -33.4 10.7 46.1 10.7 46.1 -33.4
>> 26.7
>> V11 V12 V21 V22 V11 V12 V21 V22 V11 V12 V21 V22
>> -33.4 26.7 46.1 -37.3 46.1 -37.3 26.7 1.2 26.7 1.2 -37.3
>> 36.3
>> V11 V12 V21 V22 V11 V12 V21 V22 V11 V12 V21 V22
>> -37.3 36.3 1.2 39.6 1.2 39.6 36.3 31.0 36.3 -27.3 39.6
>> 14.4
>> V11 V12 V21 V22 V11 V12 V21 V22
>> 39.6 29.0 31.0 -38.1 31.0 -3.4 -27.3 55.1
>>
>>
>> The output should be
>>
>> [[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
>>
>> ...
>> Thanks and much appreciated!
>>
>>
>>
>> Muhammad
>>
>> ______________________________________________
>> 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.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
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).
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)
it returns,
[,1] [,2]
[1,] 14.4 29.0
[2,] -38.1 -3.4
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
On Jan 29, 2010, at 12:43 PM, Muhammad Rahiz 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).
This returns,
[,1] [,2]
[1,] -27.3 14.4
[2,] 29.0 -38.1
Much simpler to do:
> xx[ , , 1]
[,1] [,2]
[1,] -27.3 14.4
[2,] 29.0 -38.1
If I do,
y2 <- matrix(xx[c(5:8)],2,2)
it returns,
[,1] [,2]
[1,] 14.4 29.0
[2,] -38.1 -3.4
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)
}
I don't get it. You had the data in a list. You wanted it out of that list, and now you're going to put it back in another list??? ( y2 would not be the same as y[[2]] ) What's wrong with xx[ , , 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
OK, I've got this. The output prints what I want, but I'm not sure if
there will be problems in further analysis because the main idea is to
convert the data from list to matrix. I'm quite concerned with how I
define xx2.
xx <- unlist(x) # Unlist from lapply + read.table
a <- seq(1,128,by=4) # creates sequence for increment in loop
xx2 <- list() # Is this the correct definition?
for (z in 1:32){
xx2[[z]] <- matrix(xx[c(a[z]:(a[z]+4))],2,2)
}
When I do,
> mode(xx2)
> [1] "list"
When I do,
> xx3 <- xx2[[1]] + 5 # simple test
> mode(xx3)
> "numeric"
Am I doing this right?
Muhammad
----------
Muhammad Rahiz 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).
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)
it returns,
[,1] [,2]
[1,] 14.4 29.0
[2,] -38.1 -3.4
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
______________________________________________ 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.
On Jan 29, 2010, at 1:07 PM, Muhammad Rahiz wrote:
OK, I've got this. The output prints what I want, but I'm not sure if there will be problems in further analysis because the main idea is to convert the data from list to matrix. I'm quite concerned with how I define xx2. xx <- unlist(x) # Unlist from lapply + read.table a <- seq(1,128,by=4) # creates sequence for increment in loop xx2 <- list() # Is this the correct definition?
It will "work".
for (z in 1:32){
xx2[[z]] <- matrix(xx[c(a[z]:(a[z]+4))],2,2) # which would be a list
of matrices
}
If you go back to your original posting, you could shortcut the whole process since you already had a list of 32 dataframes (lists) . That was the starting point. If a list is acceptable, then skip the intermediate array. > class(x[[1]]) [1] "data.frame" > class(lapply(x, data.matrix)[[1]]) [1] "matrix" So just do this: xx2 <- lapply(x, data.matrix) # a list of matrices
David.
> When I do,
> > mode(xx2)
> > [1] "list"
>
> When I do,
> > xx3 <- xx2[[1]] + 5 # simple test
> > mode(xx3)
> > "numeric"
>
>
> Am I doing this right?
>
>
> Muhammad
>
> ----------
>
> Muhammad Rahiz 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).
>>
>> 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)
>>
>> it returns,
>>
>> [,1] [,2]
>> [1,] 14.4 29.0
>> [2,] -38.1 -3.4
>>
>> 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
>>>
>>>
>>>
>>
>> ______________________________________________
>> 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.
>>
David Winsemius, MD
Heritage Laboratories
West Hartford, CT