"S3方法"是什么?在R中意味着什么? [英] What does "S3 methods" mean in R?

查看:104
本文介绍了"S3方法"是什么?在R中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我是R的新手,所以我不知道S3方法和对象是什么.我发现有S3和S4对象系统,有些建议尽可能使用S3而不是S4(请参阅

Since I am fairly new to R, I do not know what the S3 methods and objects are. I found that there are S3 and S4 object systems, and some recommend to use S3 over S4 if possible (See Google's R Style Guide at http://google-styleguide.googlecode.com/svn/trunk/google-r-style.html)*. However, I do not know the exact definition of S3 methods/objects.

更新:截至2019年,Google的《 R风格指南》超链接现在位于此处. /p>

Update: As of 2019, Google's R Style Guide hyperlink is now here.

推荐答案

通过查看?S3?UseMethod,可以找到大多数相关信息,但总而言之:

Most of the relevant information can be found by looking at ?S3 or ?UseMethod, but in a nutshell:

S3是指方法分派的方案.如果您使用R已有一段时间,您会注意到对于许多不同类型的对象,有printpredictsummary方法.

S3 refers to a scheme of method dispatching. If you've used R for a while, you'll notice that there are print, predict and summary methods for a lot of different kinds of objects.

在S3中,此方法的工作原理是:

In S3, this works by:

  • 设置对象的类别 利息(例如:a的返回值 对方法glm的调用具有类glm)
  • 通过一般方法提供一种方法 名称(例如print),然后是一个点,以及 然后是类名(例如: print.glm)
  • 必须做一些准备 以此通用名称(print)完成 为这个工作,但如果你是 只是想使自己适应 现有的方法名称,您不需要 这个(请参阅我所指的帮助 如果可以的话,请更早些.)
  • setting the class of objects of interest (e.g.: the return value of a call to method glm has class glm)
  • providing a method with the general name (e.g. print), then a dot, and then the classname (e.g.: print.glm)
  • some preparation has to have been done to this general name (print) for this to work, but if you're simply looking to conform yourself to existing method names, you don't need this (see the help I refered to earlier if you do).

在情人眼中,尤其是新创建的时髦模型拟合程序包的用户,能够键入predict(myfit, type="class")predict.mykindoffit(myfit, type="class")更加方便.

To the eye of the beholder, and particularly, the user of your newly created funky model fitting package, it is much more convenient to be able to type predict(myfit, type="class") than predict.mykindoffit(myfit, type="class").

还有很多其他功能,但这应该可以帮助您入门.这种基于对象的属性(类)的调度方法有很多缺点(C纯粹主义者可能会在晚上惊恐地醒着),但是在很多情况下,它的工作方式相当不错.在当前版本的R中,已经实现了更新的方式(S4和引用类),但是大多数人仍然(仅)使用S3.

There is quite a bit more to it, but this should get you started. There are quite a few disadvantages to this way of dispatching methods based upon an attribute (class) of objects (and C purists probably lie awake at night in horror of it), but for a lot of situations, it works decently. With the current version of R, newer ways have been implemented (S4 and reference classes), but most people still (only) use S3.

这篇关于"S3方法"是什么?在R中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆