Re: [R] Can a matrix have 'list' as rows/columns?
[snip] sapply. In this case I would expect M to be a list. I am gobsmacked that a list can be considered a vector. Is that a bug? It must be bad design? I have been using R for a number of years (5?) and heavilly for two years. I am still getting bitten by these features in R. To my mind there are many places that R violates the *principle of least surprise. But it may be my mind that is at fault! What are other people's experience?* Since a matrix may contain logical, integer, numeric (double precision), complex, and character data, I would be surprised if it didn't also handle list data. I am surprised that a matrix cannot contain factor data. logical, integer, double, complex and character are all atomic. Usually, where I have been (have I been in the wrong places?) a vector is a one dimensional ordered collection of homogeneously typed atomic things. A matrix an ordered collection of vectors, all the same type and length. A list is R's hold all type. It has a count of things in it, but each thing is an arbitrary thing in itself. It could be another list, or whatever. Elsewhere on this thread there are some helpful examples of how lists must be changed to be forced into a matrix or to be a vector. That is the violation of the Least Astonishment Principle. Here is an example I just made up... L - list() L[[1]] - list() L[[1]][[1]] - TRUE L[[1]][[ABC]] - pi L [[1]] [[1]][[1]] [1] TRUE [[1]]$ABC [1] 3.141593 V - as.vector(L) V [[1]] [[1]][[1]] [1] TRUE [[1]]$ABC [1] 3.141593 M - matrix(L, ncol=2, nrow=3) M [,1] [,2] [1,] List,2 List,2 [2,] List,2 List,2 [3,] List,2 List,2 V[2] - list() Error in V[2] - list() : replacement has length zero L2 - list() L2[[1]] - asd V[2] - L2 class(V) [1] list V2 - as.vector(V) length(V2) [1] 2 V2[1] [[1]] [[1]][[1]] [1] TRUE [[1]]$ABC [1] 3.141593 V2[2] [[1]] [1] asd That is astonishing to me! I had no way to predict what would happen. 5 days ago I would have expected the statement as.vector(L) to produce an error. V[2] - list() did. cheers Worik __ R-help@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.
Re: [R] Can a matrix have 'list' as rows/columns?
-Original Message- From: Worik R [mailto:wor...@gmail.com] Sent: Wednesday, April 18, 2012 5:05 PM To: William Dunlap Cc: r-help Subject: Re: [R] Can a matrix have 'list' as rows/columns? [snip] sapply. In this case I would expect M to be a list. I am gobsmacked that a list can be considered a vector. Is that a bug? It must be bad design? I have been using R for a number of years (5?) and heavilly for two years. I am still getting bitten by these features in R. To my mind there are many places that R violates the *principle of least surprise. But it may be my mind that is at fault! What are other people's experience?* Since a matrix may contain logical, integer, numeric (double precision), complex, and character data, I would be surprised if it didn't also handle list data. I am surprised that a matrix cannot contain factor data. logical, integer, double, complex and character are all atomic. Usually, where I have been (have I been in the wrong places?) a vector is a one dimensional ordered collection of homogeneously typed atomic things. A matrix an ordered collection of vectors, all the same type and length. It will be more fruitful in R to think of a vector as a thing that can be subscripted by one subscript and a matrix as something that can be subscripted by two. (This is not exactly how a matrix is defined, but I find it is good way to think about them.) A matrix should definitely not be thought of as an ordered collection of vectors, all the same type and length. It is just a vector with an attribute called dims that lets you subscript with 2 indices. I have used a matrix(list(...),...) to conveniently contain remote sensing observations binned by latitude and longitude. Each bin may contain any number of observations. A list is R's hold all type. It has a count of things in it, but each thing is an arbitrary thing in itself. It could be another list, or whatever. Elsewhere on this thread there are some helpful examples of how lists must be changed to be forced into a matrix or to be a vector. That is the violation of the Least Astonishment Principle. Here is an example I just made up... L - list() L[[1]] - list() L[[1]][[1]] - TRUE L[[1]][[ABC]] - pi L [[1]] [[1]][[1]] [1] TRUE [[1]]$ABC [1] 3.141593 V - as.vector(L) V [[1]] [[1]][[1]] [1] TRUE [[1]]$ABC [1] 3.141593 Note that as.vector just strips the attributes (except for the names) from the input list. Bare lists are vectors. Hence V and L are identical. M - matrix(L, ncol=2, nrow=3) M [,1] [,2] [1,] List,2 List,2 [2,] List,2 List,2 [3,] List,2 List,2 V[2] - list() Error in V[2] - list() : replacement has length zero Compare this to an atomic vector - there is no difference in behavior: a - 1 # a length 1 atomic vector a[2] - numeric() Error in a[2] - numeric() : replacement has length zero You cannot replace a 1-long part of a vector with a 0-long vector. (If you could, I suppose it would be a no-op.) With a list you can do V[[2]] - list() because V[[2]] refers to the 2nd element of the list, not the sublist of length 1 that contains the 2nd element that V[2] means. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com L2 - list() L2[[1]] - asd V[2] - L2 class(V) [1] list V2 - as.vector(V) length(V2) [1] 2 V2[1] [[1]] [[1]][[1]] [1] TRUE [[1]]$ABC [1] 3.141593 V2[2] [[1]] [1] asd That is astonishing to me! I had no way to predict what would happen. 5 days ago I would have expected the statement as.vector(L) to produce an error. V[2] - list() did. cheers Worik __ R-help@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.
Re: [R] Can a matrix have 'list' as rows/columns?
Thanks. That is clear. I need to seperate the concept of Array and Vector in my head. As soon as I hit sendon that last post I realised it is a bit silly to say vectors only contain atomic things. W On Thu, Apr 19, 2012 at 12:35 PM, William Dunlap wdun...@tibco.com wrote: -Original Message- From: Worik R [mailto:wor...@gmail.com] Sent: Wednesday, April 18, 2012 5:05 PM To: William Dunlap Cc: r-help Subject: Re: [R] Can a matrix have 'list' as rows/columns? [snip] sapply. In this case I would expect M to be a list. I am gobsmacked that a list can be considered a vector. Is that a bug? It must be bad design? I have been using R for a number of years (5?) and heavilly for two years. I am still getting bitten by these features in R. To my mind there are many places that R violates the *principle of least surprise. But it may be my mind that is at fault! What are other people's experience?* Since a matrix may contain logical, integer, numeric (double precision), complex, and character data, I would be surprised if it didn't also handle list data. I am surprised that a matrix cannot contain factor data. logical, integer, double, complex and character are all atomic. Usually, where I have been (have I been in the wrong places?) a vector is a one dimensional ordered collection of homogeneously typed atomic things. A matrix an ordered collection of vectors, all the same type and length. It will be more fruitful in R to think of a vector as a thing that can be subscripted by one subscript and a matrix as something that can be subscripted by two. (This is not exactly how a matrix is defined, but I find it is good way to think about them.) A matrix should definitely not be thought of as an ordered collection of vectors, all the same type and length. It is just a vector with an attribute called dims that lets you subscript with 2 indices. I have used a matrix(list(...),...) to conveniently contain remote sensing observations binned by latitude and longitude. Each bin may contain any number of observations. A list is R's hold all type. It has a count of things in it, but each thing is an arbitrary thing in itself. It could be another list, or whatever. Elsewhere on this thread there are some helpful examples of how lists must be changed to be forced into a matrix or to be a vector. That is the violation of the Least Astonishment Principle. Here is an example I just made up... L - list() L[[1]] - list() L[[1]][[1]] - TRUE L[[1]][[ABC]] - pi L [[1]] [[1]][[1]] [1] TRUE [[1]]$ABC [1] 3.141593 V - as.vector(L) V [[1]] [[1]][[1]] [1] TRUE [[1]]$ABC [1] 3.141593 Note that as.vector just strips the attributes (except for the names) from the input list. Bare lists are vectors. Hence V and L are identical. M - matrix(L, ncol=2, nrow=3) M [,1] [,2] [1,] List,2 List,2 [2,] List,2 List,2 [3,] List,2 List,2 V[2] - list() Error in V[2] - list() : replacement has length zero Compare this to an atomic vector - there is no difference in behavior: a - 1 # a length 1 atomic vector a[2] - numeric() Error in a[2] - numeric() : replacement has length zero You cannot replace a 1-long part of a vector with a 0-long vector. (If you could, I suppose it would be a no-op.) With a list you can do V[[2]] - list() because V[[2]] refers to the 2nd element of the list, not the sublist of length 1 that contains the 2nd element that V[2] means. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com L2 - list() L2[[1]] - asd V[2] - L2 class(V) [1] list V2 - as.vector(V) length(V2) [1] 2 V2[1] [[1]] [[1]][[1]] [1] TRUE [[1]]$ABC [1] 3.141593 V2[2] [[1]] [1] asd That is astonishing to me! I had no way to predict what would happen. 5 days ago I would have expected the statement as.vector(L) to produce an error. V[2] - list() did. cheers Worik __ R-help@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.
Re: [R] Can a matrix have 'list' as rows/columns?
That is a fine section of 'The R Inferno' but I don't think it applies to your problem. The answer to your question in the subject line is obviously yes. It happens when the matrix (or more generally any array) is of mode list. A useful example of this is in Circle 8.1.8. http://www.burns-stat.com/pages/Tutor/R_inferno.pdf In your case you can do things like: M[[1,1]] [1] aaa M[[1,2]] [1] bbb M[[2,2]] [1] 0.274498 But not: M[[,2]] Error in M[[, 2]] : invalid subscript type 'symbol' Pat On 17/04/2012 05:13, Worik R wrote: After a lot of processing I get a matrix into M. I expected each row and column to be a vector. But it is a list. R-Inferno says... Arrays (including matrices) can be subscripted with a matrix of positive numbers. The subscripting matrix has as many columns as there are dimensions in the array—so two columns for a matrix. The result is a vector (not an array) containing the selected items. My version of R: version.string R version 2.12.1 (2010-12-16) Here is an example... Qm- c(aaa, bbb, ccc) DF- data.frame(Name=sample(Qm, replace=TRUE, size=22), Value=runif(22), stringsAsFactors=FALSE) M- sapply(Qm, function(nm, DF){last(DF[DF[, Name]==nm,])}, DF) class(M) [1] matrix class(M[,1]) [1] list class(M[1,]) [1] list M aaa bbb ccc Name aaa bbbccc Value 0.4702648 0.274498 0.5529691 DF Name Value 1 ccc 0.99948920 2 aaa 0.51921281 3 aaa 0.10803943 4 aaa 0.82265847 5 ccc 0.83237260 6 bbb 0.88250933 7 aaa 0.41836131 8 aaa 0.66197290 9 ccc 0.01911771 10 ccc 0.4699 11 bbb 0.35719884 12 ccc 0.86274858 13 bbb 0.57528579 14 aaa 0.12452158 15 aaa 0.44167731 16 aaa 0.11660019 17 ccc 0.55296911 18 aaa 0.12796890 19 bbb 0.44595741 20 bbb 0.93024768 21 aaa 0.47026475 22 bbb 0.27449801 [[alternative HTML version deleted]] __ R-help@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. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@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.
Re: [R] Can a matrix have 'list' as rows/columns?
On Apr 17, 2012, at 12:13 AM, Worik R wrote: After a lot of processing I get a matrix into M. I expected each row and column to be a vector. But it is a list. This behavior is not the result of limitation in how R's sapply might have processed a purely numeric set of results, but is because you (probably) returned a hetergeneous set of classes rom you inner function. Assuming that last is actually function(x){tail,1}, then the structure of M is str(M) List of 6 $ : chr aaa $ : num 0.224 $ : chr bbb $ : num 0.768 $ : chr ccc $ : num 0.904 - attr(*, dim)= int [1:2] 2 3 - attr(*, dimnames)=List of 2 ..$ : chr [1:2] Name Value ..$ : chr [1:3] aaa bbb ccc Had the result been a more homogeneous collection, I sapply would have returned an array of atomic numeric vectors. Try just returning a number: M2 - sapply(Qm, function(nm, DF){last(DF[DF[, Name]==nm,Value])}, DF) class(M) [1] numeric str(M2) Named num [1:3] 0.6184 0.0446 0.3605 - attr(*, names)= chr [1:3] aaa bbb ccc -- David. R-Inferno says... Arrays (including matrices) can be subscripted with a matrix of positive numbers. The subscripting matrix has as many columns as there are dimensions in the arrayso two columns for a matrix. The result is a vector (not an array) containing the selected items. My version of R: version.string R version 2.12.1 (2010-12-16) Here is an example... Qm - c(aaa, bbb, ccc) DF - data.frame(Name=sample(Qm, replace=TRUE, size=22), Value=runif(22), stringsAsFactors=FALSE) M - sapply(Qm, function(nm, DF){last(DF[DF[, Name]==nm,])}, DF) class(M) [1] matrix class(M[,1]) [1] list class(M[1,]) [1] list M aaa bbb ccc Name aaa bbbccc Value 0.4702648 0.274498 0.5529691 DF Name Value 1 ccc 0.99948920 2 aaa 0.51921281 3 aaa 0.10803943 4 aaa 0.82265847 5 ccc 0.83237260 6 bbb 0.88250933 7 aaa 0.41836131 8 aaa 0.66197290 9 ccc 0.01911771 10 ccc 0.4699 11 bbb 0.35719884 12 ccc 0.86274858 13 bbb 0.57528579 14 aaa 0.12452158 15 aaa 0.44167731 16 aaa 0.11660019 17 ccc 0.55296911 18 aaa 0.12796890 19 bbb 0.44595741 20 bbb 0.93024768 21 aaa 0.47026475 22 bbb 0.27449801 [[alternative HTML version deleted]] __ R-help@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 West Hartford, CT __ R-help@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.
Re: [R] Can a matrix have 'list' as rows/columns?
On Tue, Apr 17, 2012 at 11:52 PM, David Winsemius dwinsem...@comcast.netwrote: On Apr 17, 2012, at 12:13 AM, Worik R wrote: After a lot of processing I get a matrix into M. I expected each row and column to be a vector. But it is a list. This behavior is not the result of limitation in how R's sapply might have processed a purely numeric set of results, but is because you (probably) returned a hetergeneous set of classes rom you inner function. Assuming that last is actually function(x){tail,1}, then the structure of M is str(M) List of 6 [snip] ..$ : chr [1:3] aaa bbb ccc Had the result been a more homogeneous collection, I sapply would have returned an array of atomic numeric vectors. Try just returning a number: M2 - sapply(Qm, function(nm, DF){last(DF[DF[, Name]==nm,Value])}, DF) Yes that returns a vector. I want a matrix. I see that my problem is that the columns of DF are not all the same type. Once I did that (made Value character) I get my matrix just as I need. SO it was I passed *in* that was the problem Not what I did with it inside sapply. In this case I would expect M to be a list. I am gobsmacked that a list can be considered a vector.Is that a bug? It must be bad design? I have been using R for a number of years (5?) and heavilly for two years. I am still getting bitten by these features in R. To my mind there are many places that R violates the *principle of least surprise. But it may be my mind that is at fault! What are other people's experience?* Worik class(M) [1] numeric str(M2) Named num [1:3] 0.6184 0.0446 0.3605 - attr(*, names)= chr [1:3] aaa bbb ccc -- David. R-Inferno says... Arrays (including matrices) can be subscripted with a matrix of positive numbers. The subscripting matrix has as many columns as there are dimensions in the arrayso two columns for a matrix. The result is a vector (not an array) containing the selected items. My version of R: version.string R version 2.12.1 (2010-12-16) Here is an example... Qm - c(aaa, bbb, ccc) DF - data.frame(Name=sample(Qm, replace=TRUE, size=22), Value=runif(22), stringsAsFactors=FALSE) M - sapply(Qm, function(nm, DF){last(DF[DF[, Name]==nm,])}, DF) class(M) [1] matrix class(M[,1]) [1] list class(M[1,]) [1] list M aaa bbb ccc Name aaa bbbccc Value 0.4702648 0.274498 0.5529691 DF Name Value 1 ccc 0.99948920 2 aaa 0.51921281 3 aaa 0.10803943 4 aaa 0.82265847 5 ccc 0.83237260 6 bbb 0.88250933 7 aaa 0.41836131 8 aaa 0.66197290 9 ccc 0.01911771 10 ccc 0.4699 11 bbb 0.35719884 12 ccc 0.86274858 13 bbb 0.57528579 14 aaa 0.12452158 15 aaa 0.44167731 16 aaa 0.11660019 17 ccc 0.55296911 18 aaa 0.12796890 19 bbb 0.44595741 20 bbb 0.93024768 21 aaa 0.47026475 22 bbb 0.27449801 [[alternative HTML version deleted]] __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius, MD West Hartford, CT [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Can a matrix have 'list' as rows/columns?
On Apr 17, 2012, at 7:27 PM, Worik R wrote: On Tue, Apr 17, 2012 at 11:52 PM, David Winsemius dwinsem...@comcast.net wrote: On Apr 17, 2012, at 12:13 AM, Worik R wrote: After a lot of processing I get a matrix into M. I expected each row and column to be a vector. But it is a list. This behavior is not the result of limitation in how R's sapply might have processed a purely numeric set of results, but is because you (probably) returned a hetergeneous set of classes rom you inner function. Assuming that last is actually function(x){tail,1}, then the structure of M is str(M) List of 6 [snip] ..$ : chr [1:3] aaa bbb ccc Had the result been a more homogeneous collection, I sapply would have returned an array of atomic numeric vectors. Try just returning a number: M2 - sapply(Qm, function(nm, DF){last(DF[DF[, Name]==nm,Value])}, DF) Yes that returns a vector. I want a matrix. I see that my problem is that the columns of DF are not all the same type. Once I did that (made Value character) I get my matrix just as I need. SO it was I passed *in* that was the problem Not what I did with it inside sapply. In this case I would expect M to be a list. I am gobsmacked that a list can be considered a vector.Is that a bug? No. It is by design. list is an acceptable storage mode for vector(). It must be bad design? That is (obviously) a matter of opinion. R is in the middle region between LiSP and a strongly typed language. I have been using R for a number of years (5?) and heavilly for two years. I am still getting bitten by these features in R. To my mind there are many places that R violates the *principle of least surprise. I keep getting surprises as well. I did experience surprise at the point I saw that is.vector() returning TRUE for a list. I think that means that is.vector is rather less informative than I expected. Essentially only language objects fail: z - as.formula(x ~ y) z x ~ y is.vector(z) [1] FALSE Even expressions are vectors: z - expression( x ~ y) z expression(x ~ y) is.vector(z) [1] TRUE But it may be my mind that is at fault! What are other people's experience?* I still have not fully wrapped my head around the higher levels of the language. I thought reading Chamber's book would help, but it had too much prose and did not present enough worked examples to sync with my learning style. I'm still looking for a book that lets me use the language more effectively. -- David. Worik class(M) [1] numeric str(M2) Named num [1:3] 0.6184 0.0446 0.3605 - attr(*, names)= chr [1:3] aaa bbb ccc -- David. R-Inferno says... Arrays (including matrices) can be subscripted with a matrix of positive numbers. The subscripting matrix has as many columns as there are dimensions in the arrayso two columns for a matrix. The result is a vector (not an array) containing the selected items. My version of R: version.string R version 2.12.1 (2010-12-16) Here is an example... Qm - c(aaa, bbb, ccc) DF - data.frame(Name=sample(Qm, replace=TRUE, size=22), Value=runif(22), stringsAsFactors=FALSE) M - sapply(Qm, function(nm, DF){last(DF[DF[, Name]==nm,])}, DF) class(M) [1] matrix class(M[,1]) [1] list class(M[1,]) [1] list M aaa bbb ccc Name aaa bbbccc Value 0.4702648 0.274498 0.5529691 DF Name Value 1 ccc 0.99948920 2 aaa 0.51921281 3 aaa 0.10803943 4 aaa 0.82265847 5 ccc 0.83237260 6 bbb 0.88250933 7 aaa 0.41836131 8 aaa 0.66197290 9 ccc 0.01911771 10 ccc 0.4699 11 bbb 0.35719884 12 ccc 0.86274858 13 bbb 0.57528579 14 aaa 0.12452158 15 aaa 0.44167731 16 aaa 0.11660019 17 ccc 0.55296911 18 aaa 0.12796890 19 bbb 0.44595741 20 bbb 0.93024768 21 aaa 0.47026475 22 bbb 0.27449801 David Winsemius, MD West Hartford, CT __ R-help@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.
Re: [R] Can a matrix have 'list' as rows/columns?
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Worik R Sent: Tuesday, April 17, 2012 4:28 PM To: r-help Subject: Re: [R] Can a matrix have 'list' as rows/columns? On Tue, Apr 17, 2012 at 11:52 PM, David Winsemius dwinsem...@comcast.netwrote: On Apr 17, 2012, at 12:13 AM, Worik R wrote: After a lot of processing I get a matrix into M. I expected each row and column to be a vector. But it is a list. This behavior is not the result of limitation in how R's sapply might have processed a purely numeric set of results, but is because you (probably) returned a hetergeneous set of classes rom you inner function. Assuming that last is actually function(x){tail,1}, then the structure of M is str(M) List of 6 [snip] ..$ : chr [1:3] aaa bbb ccc Had the result been a more homogeneous collection, I sapply would have returned an array of atomic numeric vectors. Try just returning a number: M2 - sapply(Qm, function(nm, DF){last(DF[DF[, Name]==nm,Value])}, DF) Yes that returns a vector. I want a matrix. I see that my problem is that the columns of DF are not all the same type. Once I did that (made Value character) I get my matrix just as I need. SO it was I passed *in* that was the problem Not what I did with it inside sapply. In this case I would expect M to be a list. I am gobsmacked that a list can be considered a vector.Is that a bug? It must be bad design? I have been using R for a number of years (5?) and heavilly for two years. I am still getting bitten by these features in R. To my mind there are many places that R violates the *principle of least surprise. But it may be my mind that is at fault! What are other people's experience?* Since a matrix may contain logical, integer, numeric (double precision), complex, and character data, I would be surprised if it didn't also handle list data. I am surprised that a matrix cannot contain factor data. z - matrix(factor(letters[1:6], levels=letters[1:10]), nrow=2) z [,1] [,2] [,3] [1,] a c e [2,] b d f str(z) chr [1:2, 1:3] a b c d e f Different things surprise different people. You might try using vapply() instead of sapply(). It forces you to supply what you expect the output of FUN(X[[i]]) to look like and will stop with an explicit error message if it doesn't match your expectation on any iteration. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com Worik class(M) [1] numeric str(M2) Named num [1:3] 0.6184 0.0446 0.3605 - attr(*, names)= chr [1:3] aaa bbb ccc -- David. R-Inferno says... Arrays (including matrices) can be subscripted with a matrix of positive numbers. The subscripting matrix has as many columns as there are dimensions in the array-so two columns for a matrix. The result is a vector (not an array) containing the selected items. My version of R: version.string R version 2.12.1 (2010-12-16) Here is an example... Qm - c(aaa, bbb, ccc) DF - data.frame(Name=sample(Qm, replace=TRUE, size=22), Value=runif(22), stringsAsFactors=FALSE) M - sapply(Qm, function(nm, DF){last(DF[DF[, Name]==nm,])}, DF) class(M) [1] matrix class(M[,1]) [1] list class(M[1,]) [1] list M aaa bbb ccc Name aaa bbbccc Value 0.4702648 0.274498 0.5529691 DF Name Value 1 ccc 0.99948920 2 aaa 0.51921281 3 aaa 0.10803943 4 aaa 0.82265847 5 ccc 0.83237260 6 bbb 0.88250933 7 aaa 0.41836131 8 aaa 0.66197290 9 ccc 0.01911771 10 ccc 0.4699 11 bbb 0.35719884 12 ccc 0.86274858 13 bbb 0.57528579 14 aaa 0.12452158 15 aaa 0.44167731 16 aaa 0.11660019 17 ccc 0.55296911 18 aaa 0.12796890 19 bbb 0.44595741 20 bbb 0.93024768 21 aaa 0.47026475 22 bbb 0.27449801 [[alternative HTML version deleted]] __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r- helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius, MD West Hartford, CT [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Can a matrix have 'list' as rows/columns?
Not sure this adds to, subtracts from, or obfuscates this thread, but ... x - list(a=1:3,b = log, c=letters[2],d=as.list(1:5)) is.vector(x) [1] TRUE dim(x) - c(2,2) is.matrix(x) [1] TRUE is.vector(x) [1] FALSE x [,1] [,2] [1,] Integer,3 b [2,] ? List,5 x[2,1](5) Error: attempt to apply non-function ## because... x[2,1] [[1]] function (x, base = exp(1)) .Primitive(log) ## thus ... x[2,1][[1]](5) [1] 1.609438 ## as well as x[[2]](5) [1] 1.609438 ## because it's still the case that ... is.list(x) [1] TRUE ## it's a list with dim attribute and thus... class(x) [1] matrix ## But it's not a vector: is.vector(x) [1] FALSE I'm not sure all this is consistent, but I think a full understanding of the semantics probably requires poking into more musty corners of R than I can handle, anyway. Cheers, Bert On Tue, Apr 17, 2012 at 5:15 PM, David Winsemius dwinsem...@comcast.net wrote: On Apr 17, 2012, at 7:27 PM, Worik R wrote: On Tue, Apr 17, 2012 at 11:52 PM, David Winsemius dwinsem...@comcast.netwrote: On Apr 17, 2012, at 12:13 AM, Worik R wrote: After a lot of processing I get a matrix into M. I expected each row and column to be a vector. But it is a list. This behavior is not the result of limitation in how R's sapply might have processed a purely numeric set of results, but is because you (probably) returned a hetergeneous set of classes rom you inner function. Assuming that last is actually function(x){tail,1}, then the structure of M is str(M) List of 6 [snip] ..$ : chr [1:3] aaa bbb ccc Had the result been a more homogeneous collection, I sapply would have returned an array of atomic numeric vectors. Try just returning a number: M2 - sapply(Qm, function(nm, DF){last(DF[DF[, Name]==nm,Value])}, DF) Yes that returns a vector. I want a matrix. I see that my problem is that the columns of DF are not all the same type. Once I did that (made Value character) I get my matrix just as I need. SO it was I passed *in* that was the problem Not what I did with it inside sapply. In this case I would expect M to be a list. I am gobsmacked that a list can be considered a vector. Is that a bug? No. It is by design. list is an acceptable storage mode for vector(). It must be bad design? That is (obviously) a matter of opinion. R is in the middle region between LiSP and a strongly typed language. I have been using R for a number of years (5?) and heavilly for two years. I am still getting bitten by these features in R. To my mind there are many places that R violates the *principle of least surprise. I keep getting surprises as well. I did experience surprise at the point I saw that is.vector() returning TRUE for a list. I think that means that is.vector is rather less informative than I expected. Essentially only language objects fail: z - as.formula(x ~ y) z x ~ y is.vector(z) [1] FALSE Even expressions are vectors: z - expression( x ~ y) z expression(x ~ y) is.vector(z) [1] TRUE But it may be my mind that is at fault! What are other people's experience?* I still have not fully wrapped my head around the higher levels of the language. I thought reading Chamber's book would help, but it had too much prose and did not present enough worked examples to sync with my learning style. I'm still looking for a book that lets me use the language more effectively. -- David. Worik class(M) [1] numeric str(M2) Named num [1:3] 0.6184 0.0446 0.3605 - attr(*, names)= chr [1:3] aaa bbb ccc -- David. R-Inferno says... Arrays (including matrices) can be subscripted with a matrix of positive numbers. The subscripting matrix has as many columns as there are dimensions in the array—so two columns for a matrix. The result is a vector (not an array) containing the selected items. My version of R: version.string R version 2.12.1 (2010-12-16) Here is an example... Qm - c(aaa, bbb, ccc) DF - data.frame(Name=sample(Qm, replace=TRUE, size=22), Value=runif(22), stringsAsFactors=FALSE) M - sapply(Qm, function(nm, DF){last(DF[DF[, Name]==nm,])}, DF) class(M) [1] matrix class(M[,1]) [1] list class(M[1,]) [1] list M aaa bbb ccc Name aaa bbb ccc Value 0.4702648 0.274498 0.5529691 DF Name Value 1 ccc 0.99948920 2 aaa 0.51921281 3 aaa 0.10803943 4 aaa 0.82265847 5 ccc 0.83237260 6 bbb 0.88250933 7 aaa 0.41836131 8 aaa 0.66197290 9 ccc 0.01911771 10 ccc 0.4699 11 bbb 0.35719884 12 ccc 0.86274858 13 bbb 0.57528579 14 aaa 0.12452158 15 aaa 0.44167731 16 aaa 0.11660019 17 ccc 0.55296911 18 aaa 0.12796890 19 bbb 0.44595741 20 bbb 0.93024768 21 aaa 0.47026475 22 bbb 0.27449801 David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting
[R] Can a matrix have 'list' as rows/columns?
After a lot of processing I get a matrix into M. I expected each row and column to be a vector. But it is a list. R-Inferno says... Arrays (including matrices) can be subscripted with a matrix of positive numbers. The subscripting matrix has as many columns as there are dimensions in the arrayso two columns for a matrix. The result is a vector (not an array) containing the selected items. My version of R: version.string R version 2.12.1 (2010-12-16) Here is an example... Qm - c(aaa, bbb, ccc) DF - data.frame(Name=sample(Qm, replace=TRUE, size=22), Value=runif(22), stringsAsFactors=FALSE) M - sapply(Qm, function(nm, DF){last(DF[DF[, Name]==nm,])}, DF) class(M) [1] matrix class(M[,1]) [1] list class(M[1,]) [1] list M aaa bbb ccc Name aaa bbbccc Value 0.4702648 0.274498 0.5529691 DF Name Value 1 ccc 0.99948920 2 aaa 0.51921281 3 aaa 0.10803943 4 aaa 0.82265847 5 ccc 0.83237260 6 bbb 0.88250933 7 aaa 0.41836131 8 aaa 0.66197290 9 ccc 0.01911771 10 ccc 0.4699 11 bbb 0.35719884 12 ccc 0.86274858 13 bbb 0.57528579 14 aaa 0.12452158 15 aaa 0.44167731 16 aaa 0.11660019 17 ccc 0.55296911 18 aaa 0.12796890 19 bbb 0.44595741 20 bbb 0.93024768 21 aaa 0.47026475 22 bbb 0.27449801 [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Can a matrix have 'list' as rows/columns?
On Mon, Apr 16, 2012 at 9:13 PM, Worik R wor...@gmail.com wrote: After a lot of processing I get a matrix into M. I expected each row and column to be a vector. But it is a list. Lists are also vectors, e.g. x - list() is.vector(x) [1] TRUE y - vector(list, length=3) str(y) List of 3 $ : NULL $ : NULL $ : NULL See ?list /HB R-Inferno says... Arrays (including matrices) can be subscripted with a matrix of positive numbers. The subscripting matrix has as many columns as there are dimensions in the array—so two columns for a matrix. The result is a vector (not an array) containing the selected items. My version of R: version.string R version 2.12.1 (2010-12-16) Here is an example... Qm - c(aaa, bbb, ccc) DF - data.frame(Name=sample(Qm, replace=TRUE, size=22), Value=runif(22), stringsAsFactors=FALSE) M - sapply(Qm, function(nm, DF){last(DF[DF[, Name]==nm,])}, DF) class(M) [1] matrix class(M[,1]) [1] list class(M[1,]) [1] list M aaa bbb ccc Name aaa bbb ccc Value 0.4702648 0.274498 0.5529691 DF Name Value 1 ccc 0.99948920 2 aaa 0.51921281 3 aaa 0.10803943 4 aaa 0.82265847 5 ccc 0.83237260 6 bbb 0.88250933 7 aaa 0.41836131 8 aaa 0.66197290 9 ccc 0.01911771 10 ccc 0.4699 11 bbb 0.35719884 12 ccc 0.86274858 13 bbb 0.57528579 14 aaa 0.12452158 15 aaa 0.44167731 16 aaa 0.11660019 17 ccc 0.55296911 18 aaa 0.12796890 19 bbb 0.44595741 20 bbb 0.93024768 21 aaa 0.47026475 22 bbb 0.27449801 [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.