2

Here's my code

pulse <- round(rnorm(22, 70, 10 / 3)) + rep(c(0, 5), c(10, 12))
group <- rep(c("A", "B"), c(10, 12))

tapply(pulse, group, length)

A  B 
10 12

list<-split(pulse,group)
sapply(list,length)

A  B 
10 12
identical(tapply(pulse, group, length),sapply(list,length))#FALSE
identical(tapply(pulse, group, length),as.table(sapply(list,length)))#FALSE
identical(tapply(pulse, group, length),as.vector(sapply(list,length)))#FALSE
identical(as.table(tapply(pulse, group, length)),as.table(sapply(list,length)))#TRUE

These two functions generate same results, but why they are not identical? I used typeof() in R, it seems that both of the results with the type "double".

Why identical(tapply(pulse, group, length),sapply(list,length)) is False? How to adjust my code to make them identical?

Thank you.

SongTianyang
  • 139
  • 3
  • 9
  • 1
    If you check the `dput` of each output, one had the `.Names` attributes, while the other it is `.Dimnames`. By removing the attributes, it will be identical.. ie. ` identical(as.vector(r1), as.vector(r2))# [1] TRUE` where v1, v2 are the output of `tapply` and `sapply`. Or change the attributes i.e. `attributes(r1) <- attributes(r2); identical(r1, r2)` – akrun Apr 11 '15 at 01:44
  • Thank you so much. Turns out these two methods have different classes. It works now. – SongTianyang Apr 11 '15 at 04:01

1 Answers1

1

If we check the str of each output

str(r1)
# int [1:2(1d)] 10 12
#- attr(*, "dimnames")=List of 1
# ..$ : chr [1:2] "A" "B"
str(r2)
# Named int [1:2] 10 12
# - attr(*, "names")= chr [1:2] "A" "B"

the attributes are different between them. One way would to remove the attributes of each output, and they will be identical

 identical(as.vector(r1), as.vector(r2))
 #[1] TRUE

But, as.vector also removes the names. If we need to keep the names part, make the attributes same for the outputs

 attributes(r1) <- attributes(r2)
 identical(r1, r2)
 #[1] TRUE

where

r1 <- tapply(pulse, group, length)
r2 <- sapply(list,length)
akrun
  • 874,273
  • 37
  • 540
  • 662