分数矢量(来自MASS)如何进行错误检查?

Take the following code that generates a fractions vector:

> example<-MASS:: as.fractions(c(0.2,0.4))
> example
[1] 1/5 2/5

如果要用字符串替换这些分数之一,则会出现以下错误:

> example[1]<-"0/1"
Error in floor(x) : non-numeric argument to mathematical function

How does this happen? As far as I was concerned, fractions vectors aren't S4 objects and shouldn't have any sort of error checking, so how is floor(x) being ran when I'm trying to replace an entry in such a a vector?

评论
  • aeius
    aeius 回复

    fractions has a [<- method:

    library(MASS)
    methods(class = "fractions")
    ## [1] [            [<-          as.character Math         Ops         
    ## [6] print        Summary      t           
    ## see '?methods' for accessing help and source code