The code below illustrates some points about results from tapply that
I find strange. I wonder if they are intended and if so why it is so.
1) When you make a table the dimnames is a *named* list, tapply
returns an unnamed list.
2) data.frame behaves differently on an array and a table. Is this
an intended feature?
3) For tables class(TAB) and attr(TAB,"class") both return
"table",
but class(ARR) returns "array" whereas attr(TAB,"class")
returns
NULL.
Bendix Carstensen
----------------------
Bendix Carstensen
Senior Statistician
Steno Diabetes Center
Niels Steensens Vej 2
DK-2820 Gentofte
Denmark
tel: +45 44 43 87 38
mob: +45 30 75 87 38
fax: +45 44 43 07 06
bxc at steno.dk
www.biostat.ku.dk/~bxc
----------------------
Code:
------------------------------------------------------------------------
-
Aye <- sample( c("Yes","Si","Oui"), 177,
replace=TRUE )
Bee <- sample( c("Hum","Buzz"), 177, replace=TRUE )
Sea <- sample(
c("White","Black","Red","Dead"), 177,
replace=TRUE )
pyr <- rnorm( 177, 57, 14 )
TAB <- table( Aye, Bee, Sea )
ARR <- tapply( pyr, list( Aye, Bee, Sea ), sum )
dimnames( TAB )
dimnames( ARR )
data.frame( TAB )
data.frame( ARR )
data.frame( as.table( ARR ) )
class( TAB )
attr( TAB, "class" )
class( ARR )
attr( ARR, "class" )
------------------------------------------------------------------------
---
Result:
------------------------------------------------------------------------
---
> version
_
platform i386-pc-mingw32
arch i386
os mingw32
system i386, mingw32
status
major 2
minor 0.1
year 2004
month 11
day 15
language R > Aye <- sample( c("Yes","Si","Oui"), 177,
replace=TRUE )
> Bee <- sample( c("Hum","Buzz"), 177, replace=TRUE )
> Sea <- sample(
c("White","Black","Red","Dead"), 177,
replace=TRUE )
> pyr <- rnorm( 177, 57, 14 )
> TAB <- table( Aye, Bee, Sea )
> ARR <- tapply( pyr, list( Aye, Bee, Sea ), sum )
> dimnames( TAB )
$Aye
[1] "Oui" "Si" "Yes"
$Bee
[1] "Buzz" "Hum"
$Sea
[1] "Black" "Dead" "Red" "White"
> dimnames( ARR )
[[1]]
[1] "Oui" "Si" "Yes"
[[2]]
[1] "Buzz" "Hum"
[[3]]
[1] "Black" "Dead" "Red" "White"
>
> data.frame( TAB )
Aye Bee Sea Freq
1 Oui Buzz Black 2
2 Si Buzz Black 11
3 Yes Buzz Black 10
4 Oui Hum Black 7
5 Si Hum Black 11
6 Yes Hum Black 3
7 Oui Buzz Dead 3
8 Si Buzz Dead 11
9 Yes Buzz Dead 5
10 Oui Hum Dead 11
11 Si Hum Dead 3
12 Yes Hum Dead 5
13 Oui Buzz Red 5
14 Si Buzz Red 4
15 Yes Buzz Red 8
16 Oui Hum Red 11
17 Si Hum Red 9
18 Yes Hum Red 8
19 Oui Buzz White 10
20 Si Buzz White 7
21 Yes Buzz White 6
22 Oui Hum White 8
23 Si Hum White 11
24 Yes Hum White 8> data.frame( ARR )
Buzz.Black Hum.Black Buzz.Dead Hum.Dead Buzz.Red Hum.Red Buzz.White
Hum.White
1 130.5520 377.0875 177.9144 590.1498 346.4448 633.0975 492.9589
424.1464
2 607.4388 635.3015 651.3304 162.9175 223.1840 531.5512 347.7185
622.0213
3 555.8886 177.8216 295.0027 297.4550 417.6824 461.2143 359.4606
432.6165> data.frame( as.table( ARR ) )
Var1 Var2 Var3 Freq
1 Oui Buzz Black 130.5520
2 Si Buzz Black 607.4388
3 Yes Buzz Black 555.8886
4 Oui Hum Black 377.0875
5 Si Hum Black 635.3015
6 Yes Hum Black 177.8216
7 Oui Buzz Dead 177.9144
8 Si Buzz Dead 651.3304
9 Yes Buzz Dead 295.0027
10 Oui Hum Dead 590.1498
11 Si Hum Dead 162.9175
12 Yes Hum Dead 297.4550
13 Oui Buzz Red 346.4448
14 Si Buzz Red 223.1840
15 Yes Buzz Red 417.6824
16 Oui Hum Red 633.0975
17 Si Hum Red 531.5512
18 Yes Hum Red 461.2143
19 Oui Buzz White 492.9589
20 Si Buzz White 347.7185
21 Yes Buzz White 359.4606
22 Oui Hum White 424.1464
23 Si Hum White 622.0213
24 Yes Hum White 432.6165>
> class( TAB )
[1] "table"> attr( TAB, "class" )
[1] "table"> class( ARR )
[1] "array"> attr( ARR, "class" )
NULL