[R] count appearence of zero in a vector

Suzen, Mehmet msuzen at gmail.com
Fri Jan 4 18:26:57 CET 2013


On 4 January 2013 17:47, Bert Gunter <gunter.berton at gene.com> wrote:
> Inline.
>
> On Fri, Jan 4, 2013 at 7:44 AM, Suzen, Mehmet <msuzen at gmail.com> wrote:
>>
>> I am always reserved about types and not sure how R auto casting works
>> internally.
>> In a large code using many different packages, I think being reserved
>> about
>> this would not hurt.
>>
>> Also, are there anyway to force R to be "strongly typed" similar to
>> Occaml etc...
>
>
> Yes (I think). Use S4. See. e.g. Chambers: Software for Data Analysis"
> Programming in R
>

Thank you Bert for point out S4. Here is one trivial example:


> setClass("integerVector", representation(x="integer"))
> iv<-new("integerVector")
> iv at x <- c(1, 0, 1, 0, 2, 3)
Error in checkSlotAssignment(object, name, value) :
  assignment of an object of class “numeric” is not valid for slot ‘x’
in an object of class “integerVector”; is(value, "integer") is not
TRUE
> iv at x <- as.integer(c(1, 0, 1, 0, 2, 3))

So this is pretty safe:

> sum(iv at x == 0)
[1] 2




More information about the R-help mailing list