R (programming language): Difference between revisions

Content deleted Content added
Added a code sample that applied the preceding suggestions for readability: using intermediate values, defining sub-tasks, and clearly labelling objects
Line 352:
 
=== Object-oriented programming ===
The R language has native support for [[object-oriented programming]]. There are two native [[Application framework|frameworks]], the so-called S3 and S4 systems. The former, being more informal, supports single dispatch on the first argument, and objects are assigned to a class bysimply justby setting a "class" attribute in each object. The latter is a system like the [[CLOS|Common Lisp Object System (CLOS)-like system]], ofwith formal classes (also derived from [[S (programming language)#S4|S]]) and generic methods, thatwhich supports [[multiple dispatch]] and [[multiple inheritance]]<ref>{{cite web|url=https://stat.ethz.ch/R-manual/R-devel/library/base/html/UseMethod.html|title=Class Methods|access-date=2024-04-25}}</ref>
 
In the example below, <code>summary()</code> is a [[generic function]] that dispatches to different methods depending on whether its [[Argument of a function|argument]] is a numeric [[Vector (mathematics and physics)|vector]] or a "''factor"'':
<syntaxhighlight lang="rout">
> data <- c("a", "b", "c", "a", NA)