While discussing S4 prototypes, Hadley states that
An empty value for age is probably not what you want
Instead, he sets a default value to NA
. Here is the code
setClass("Person", representation(name = "character", age = "numeric"))
To access slots of an S4 object you use @, not $:
hadley <- new("Person", name = "Hadley")
hadley@age
# numeric(0)
An empty value for age is probably not what you want, so you can also assign a default prototype for the class:
setClass("Person", representation(name = "character", age = "numeric"),
prototype(name = NA_character_, age = NA_real_))
hadley <- new("Person", name = "Hadley")
hadley@age
# [1] NA
What are the advantages of setting a NA
prototype instead of leaving it empty?
Doesn't it only affect the way you check for whether the slot has a non default value? i.e. length(hadley@age) == 0
vs is.na(hadley@age)
?
NA
s giveNA
by default. Assume that you add the following code using your first definition ofPerson
:Alex<-new("Person", name = "Alex",age=99); mean(unlist(lapply(list(hadley,Alex),function(person) person@age)));
. It could be that you incorrectly infer that the average age in your sample is99
, whereas you do not know Hadley's age... – cryo111