> 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.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? 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" "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.99994699 > >> 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<https://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.