# [R] sapply behavior

Elizabeth Purdom epurdom at stanford.edu
Tue Sep 28 02:08:09 CEST 2004

```Hi,

I use sapply very frequently, but I have recently noticed a behavior of
sapply which I don't understand and have never seen before. Basically,
sapply returns what looks like a matrix,  says it a matrix, and appears to
let me do matrix things (like transpose). But it is also a list and behaves
like a list when I subset it, not a vector (so I can't sort a row for
instance). I don't know where this is coming from so as to avoid it, nor
how to handle the beast that sapply is returning. I double checked my old
version of R and apparently this same thing happens in 1.8.0, though I
never experienced it. I had a hard time reproducing it, and I don't know
what's setting it off, but the code below seems to do it for me. (I'm using
R on Windows XP, either 1.8.0 or 1.9.1)

Thanks for any help,
Elizabeth Purdom

> temp2<-matrix(sample(1:6,6,replace=F),byrow=F,nrow=6,ncol=4)
> colnames(temp2)<-paste("A",as.character(1:4),sep="")
> temp2<-as.data.frame(temp2)
>
newtemp2<-sapply((1:6),function(x){xmat<-temp2[temp2[,1]==x,,drop=F];return(xmat[1,])})
> print(newtemp2) #looks like matrix
[,1] [,2] [,3] [,4] [,5] [,6]
A1 1    2    3    4    5    6
A2 1    2    3    4    5    6
A3 1    2    3    4    5    6
A4 1    2    3    4    5    6
> is.matrix(newtemp2) #says it's matrix
[1] TRUE
> class(newtemp2)
[1] "matrix"
> is.list(newtemp2) #but also list
[1] TRUE
> newtemp2[,1] #can't subset and get a vector back; same thing happens for
rows.
\$A1
[1] 1

\$A2
[1] 1

\$A3
[1] 1

\$A4
[1] 1
> names(newtemp2)
NULL
> dimnames(newtemp2)
[[1]]
[1] "A1" "A2" "A3" "A4"

[[2]]
NULL
> dim(newtemp2)
[1] 4 6
> length(newtemp2)
[1] 24

```