S4 vs S3

33 views
Skip to first unread message

Balal Ezanloo

unread,
Apr 20, 2017, 9:54:13 AM4/20/17
to R-help-archive
Hi
can any one explain the difference between s4 and s3 classes in R in a simple way?

Jeff Newmiller

unread,
Apr 20, 2017, 12:44:04 PM4/20/17
to r-h...@r-project.org, Balal Ezanloo, R-help-archive
A simple explanation inevitably omits information. Whether the omitted information would have been useful to you is something only you can judge, which means you end up having to review the details anyway. Hadley Wickham's Advanced R is worth Googling, and don't forget to RTFM.

In a nutshell, S3 builds on free-typed lists of object members, with a class attribute of character type that is matched up with standard functions serving as methods using a naming convention based on pasting the method name with the class name. Generic method dispatch is determined by the class attribute of the first argument to the function. It is lightweight and easy to work with, but not type-safe, so don't shoot yourself in the foot.

S4 uses a special class data type with member "slots" accessed using a special "@" notation, created using a "class factory" approach. All methods and members are type-safe (cannot use the wrong data type with them). Generic method dispatch is according to the whole argument list type signature. I have found it to be noticeably slower than S3, but that result likely depends strongly on your use case.

If you simply must have something similar to Java/C++ class inheritance structure with pass-by-reference semantics, you might want to consider the R6 package, but the best choice is usually the simplest, S3.
--
Sent from my phone. Please excuse my brevity.
>______________________________________________
>R-h...@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-h...@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Balal Ezanloo

unread,
Apr 20, 2017, 1:35:38 PM4/20/17
to R-help-archive

balal izanloo

unread,
Apr 20, 2017, 1:49:12 PM4/20/17
to Doran, Harold, R-help-archive
Thanks
in my working with R and running some analysis, frequently i find s3 or s4 class. i am not a programer so i am confused about s3 or s4. can you show me by simple example the difference of s3 and s4 class?

On Thu, Apr 20, 2017 at 7:16 PM, Doran, Harold <HDo...@air.org> wrote:
In order to achieve what objective? I could "simply" say S4 is a more formal method of OO than S3. But, I think you might get a bit more help if you can be clear about what your end goal is.
Reply all
Reply to author
Forward
0 new messages