[R] str and structable error
Martin Maechler
maechler at stat.math.ethz.ch
Mon Nov 28 10:22:21 CET 2005
>>>>> "Petr" == Petr Pikal <petr.pikal at precheza.cz>
>>>>> on Mon, 28 Nov 2005 08:38:07 +0100 writes:
Petr> Hallo I encountered a behaviour which puzzles me (but
Petr> finally I did get what I wanted).
Petr> I used structable and strucplot but I wanted to change
Petr> names of variables in structable object.
structable is not "part of R" :
You should tell us that you are using an extra package where
structable is from : namely -- I did your homework -- "vcd"
Petr> I tried to subset it, use names but to no avail. So I
Petr> tried str and expected to get a structure of an object
Petr> but:
>> sss<-structable(Titanic)
>> str(sss)
Petr> Error in "[.structable"(x, args[[1]], ) : subscript
Petr> out of bounds
yes, and similar problems from
library(vcd)
example(structable)
str(hec)
--> see below
Petr> Finally I learned, that I need to change attributes of
Petr> structable object.
Petr> Is this error message OK and I did not read
Petr> documentation properly? Or is it normal that str gives
Petr> an error on some objects but I just was not so lucky
Petr> to meet one?.
No, it is not normal (and is a bug -- in vcd code IMO):
The reason is that structable ``objects'' (S3)
do not fulfill a fundamental property that all S (and hence R)
objects should fulfill IMO {but read on before protesting}
>>>>> length() and "[" should be compatible
>>>>> -------------------------------------
namely, for an object 'x', if
n <- length(x)
and assume n > 0 for the moment,
then
x[j]
should return something reasonable for all numeric vectors 'j'
which have values in {1,2,...,n} (and also for {-n,...,-1})
This is unfortunately not at all true for structable objects.
I'd say the authors of "structable" made a bit a peculiar
decision when they designed the "[.structable" method since that
invalidates the above basic principle.
This is particularly unfortunate, since "structable" also
inherits from "ftable" {a ``standard R'' S3 class} which does
not have that bad property
If the current "[" (non-S-like IMO) behavior of structable
objects really will be maintained in the future,
one solution / workaround would be to define a simple
str.structable method -- which would also help you for the
moment :
str.structable <- function(object, ...) {
cat("structable ")
class(object) <- class(object)[-1]
str(object, ...)
}
> str(structable(Titanic))
structable ftable [1:8, 1:4] 0 118 0 154 35 387 0 670 5 57 ...
- attr(*, "dnames")=List of 4
..$ Class : chr [1:4] "1st" "2nd" "3rd" "Crew"
..$ Sex : chr [1:2] "Male" "Female"
..$ Age : chr [1:2] "Child" "Adult"
..$ Survived: chr [1:2] "No" "Yes"
- attr(*, "split_vertical")= logi [1:4] FALSE TRUE FALSE TRUE
- attr(*, "col.vars")=List of 2
..$ Sex : chr [1:2] "Male" "Female"
..$ Survived: chr [1:2] "No" "Yes"
- attr(*, "row.vars")=List of 2
..$ Class: chr [1:4] "1st" "2nd" "3rd" "Crew"
..$ Age : chr [1:2] "Child" "Adult"
- attr(*, "class")= chr "ftable"
>
Regards,
Martin Maechler, ETH Zurich
Petr> W2000, R2.2.0, vcd package Built: R 2.2.0; ; 2005-11-
Petr> 22 14:23:44; windows,
Petr> Best regards.
Petr> Petr
Petr> Petr Pikal petr.pikal at precheza.cz
More information about the R-help
mailing list