Closed ReeceGoding closed 3 years ago
Thanks for the valuable comments! Most are spot on, and I will correct the problems. I do differ with you on two points. First, AFAIK no one considers an R factor to be an S3 object. Second, regarding the 'mtcars' dataset, the name alludes to the fact that the data comes from Motor Trend, a popular American car magazine in the 1960s/70s; the weights are almost certainly in pounds, just like the gas mileage is in miles/gallon rather than say km/liter.
Addressing your points:
1) In my experience, people do consider factors to be S3 objects. This is exactly why a lot of R tutorials warn about how they sometimes act like integers and sometimes act like characters. A well-known example is section 8.2. of the R Inferno. The documentation for factors also contains many such warnings. Regardless, whether or not factors are S3 objects is not a matter of opinion. Run methods(class=factor)
and you will see many generic functions that dispatch to them.
2) According to ??datasets::mtcars
, the wt
variable is "Weight (1000 lbs)". I think we're both right here. Yes, they're pounds, but the reader ought to be told that adding 100 pounds means adding 0.1.
R factors can be both very powerful and highly annoying. In my 'regtools' package (here on GitHub), I actually have 5 or 6 utilities to deal with problems arising with factors. But I've never heard anyone consider factors to be S3. I've been active in R since almost the beginning (and was an S user before R), have served in editorial capacity with 2 R-related journals etc.
I was recently reading the S3 section and found a number of surprises that I would like to bring to your attention:
abline
, suggesting that it is an S3 generic. This is not the case. If you typeabline
in to your console, you will find that its generic behaviour is hard-coded.cfs <- lmout$coefficients; abline(a = cfs[1], b = cfs[2])
. This is a non-example ofabline
's generic behavior, it is only showing the reader how to extract the coefficients that the section used in its first example. An actual demonstration of its generic behaviour would beabline(lmout)
.abline
is a non-example,str
is an S3 generic used in this section. No attention was drawn to this fact.Otherwise, good job! I've been enjoying this read.