[Rd] print problem with data frames (PR#698)
Duncan Murdoch
murdoch@stats.uwo.ca
Mon, 16 Oct 2000 10:31:48 -0400
On Mon, 16 Oct 2000 15:01:28 +0200 (MET DST), you wrote in message
<200010161301.PAA02685@pubhealth.ku.dk>:
>Another one from the student computer practicals: it is possible
>to create a dataframe that cannot be printed. Thus
>
>> fred <- data.frame(happy=c(TRUE, FALSE, TRUE), sad=7:9)
>> tmp <- fred[c(FALSE, FALSE, TRUE, TRUE)] # no error message!
>> tmp
>Error in as.matrix.data.frame(x) : dim<- length of dims do not match the length of object
...
>This obviously relates to my previous posting to r-devel about
>subscripting objects with logical vectors that are not the right
>length.
In this case the problem is that the indexing is missing a comma. You
should have had
tmp <- fred[c(FALSE, FALSE, TRUE, TRUE),]
because that's how you select cases. Without the comma, you're
treating the data.frame as a list containing two entries ("happy" and
"sad"), then creating a new one consisting of the 3rd and 4th entries
of that list (which are NA). It still has the data.frame class though,
so you get the error when you try to print it.
I think it might be nice to warn people about that kind of indexing,
because it's rarely intentional, but it would be really hard to do.
You'd need something like a "novice warning level" option, and writers
of procedures would have to anticipate all the common errors novices
would make.
Duncan Murdoch
P.S. to the developers: When I tried to copy and paste Jonathan's code
into a newly opened copy of R using the Edit menu, I got an error I
haven't seen before when I tried to open the menu: "No selection". I
can't reproduce this reliably yet, but I'll let you know if I figure
out what triggered it.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._