[R] sapply behavior
Prof Brian Ripley
ripley at stats.ox.ac.uk
Tue Sep 28 08:20:21 CEST 2004
On Mon, 27 Sep 2004, Liaw, Andy wrote:
> The problem is that temp2 is a data frame, and the function you are
> sapply()ing to returns a row from a data frame. A data frame is really a
> list, with each variable corresponding to a component. If you extract a row
> of a data frame, you get another data frame, not a vector, even if all
> variables are the same type. sapply() can really `simplify' the right way
> if it's given a vector (or matrix). Consider:
>
> > str(temp2)
> `data.frame': 6 obs. of 4 variables:
> $ A1: int 5 2 4 6 1 3
> $ A2: int 5 2 4 6 1 3
> $ A3: int 5 2 4 6 1 3
> $ A4: int 5 2 4 6 1 3
> > temp2 <- as.matrix(temp2)
> > str(temp2)
> int [1:6, 1:4] 5 2 4 6 1 3 5 2 4 6 ...
> - attr(*, "dimnames")=List of 2
> ..$ : chr [1:6] "1" "2" "3" "4" ...
> ..$ : chr [1:4] "A1" "A2" "A3" "A4"
> > str(sapply(1:6,function(x){xmat<-temp2[temp2[,1]==x,,drop=F]; xmat[1,]}))
> int [1:4, 1:6] 1 1 1 1 2 2 2 2 3 3 ...
> - attr(*, "dimnames")=List of 2
> ..$ : chr [1:4] "A1" "A2" "A3" "A4"
> ..$ : NULL
>
> (The is.matrix() function probably just check whether the dim attribute is a
> vector of length 2, and not a data frame (as it says in ?is.matrix). The
> newtemp2 object you get is a list with 24 components, each component is a
> vector of one integer, and has a dim attribute of c(4, 6). Not what I would
> call a matrix.)
That *is* a matrix, though, and is useful for lists of length greater than
one. A matrix in R is just a vector with a dim attribute (and length the
product of the dims's), so as well as any of the atomic vectors it can be
a generic vector aka list.
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list