请选择 进入手机版 | 继续访问电脑版
楼主: oliyiyi
1404 9

How to create S3 classes in R [推广有奖]

版主

泰斗

0%

还不是VIP/贵宾

-

TA的文库  其他...

计量文库

威望
7
论坛币
271951 个
通用积分
31269.3519
学术水平
1435 点
热心指数
1554 点
信用等级
1345 点
经验
383775 点
帖子
9598
精华
66
在线时间
5468 小时
注册时间
2007-5-21
最后登录
2024-4-18

初级学术勋章 初级热心勋章 初级信用勋章 中级信用勋章 中级学术勋章 中级热心勋章 高级热心勋章 高级学术勋章 高级信用勋章 特级热心勋章 特级学术勋章 特级信用勋章

oliyiyi 发表于 2019-1-12 11:37:17 |显示全部楼层 |坛友微信交流群

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

First, everything in R is treated like as an object. We have seen this with functions. Many of the objects that are created within an R session have attributes associated with them. One common attribute associated with an object is its class.

You can set the class attribute using the class command. One thing to notice is that the class is a vector which allows an object to inherit from multiple classes, and it allows you to specify the order of inheritance for complex classes. You can also use the class command to determine the classes associated with an object.

  1. > bubba <- c(1,2,3)
  2. > bubba
  3. [1] 1 2 3
  4. >
  5. > class(bubba)
  6. [1] "numeric"
  7. >
  8. > class(bubba) <- append(class(bubba),"Flamboyancy")
  9. > class(bubba)
  10. [1] "numeric"     "Flamboyancy"
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:classes Create creat ATE CRE

已有 1 人评分学术水平 热心指数 信用等级 收起 理由
Stakiny + 3 + 3 + 3 精彩帖子

总评分: 学术水平 + 3  热心指数 + 3  信用等级 + 3   查看全部评分

缺少币币的网友请访问有奖回帖集合
https://bbs.pinggu.org/thread-3990750-1-1.html
oliyiyi 发表于 2019-1-12 11:37:42 |显示全部楼层 |坛友微信交流群
A new command, append, is used here. The first argument is a vector, and the function adds the following arguments to the end of the vector.

使用道具

oliyiyi 发表于 2019-1-12 11:43:25 |显示全部楼层 |坛友微信交流群
One way to define a method for a class is to use the UseMethod command to define a hierarchy of functions that will react appropriately. The UseMethod command will tell R to look for a function whose prefix matches the current function, and it searches for a suffix in order from the vector of class names. In other words a set of functions can be defined, and the function that is actually called will be determined by the class name of the first object in the list of arguments.

You first have to define a generic function to reserve the function name. The UseMethod command is then used to tell the R system to search for a different function. The search is based on the name of the function and the names of an object’s classes. The name of the functions have two parts separated by a ”.” where the prefix is the function name and the suffix is the name of a class.

That is a lot of verbiage to describe a relatively simple idea. A very basic example is given below:

使用道具

oliyiyi 发表于 2019-1-12 11:43:44 |显示全部楼层 |坛友微信交流群
  1. > bubba <- list(first="one", second="two", third="third")
  2. > class(bubba) <- append(class(bubba),"Flamboyancy")
  3. >
  4. > bubba
  5. $first
  6. [1] "one"

  7. $second
  8. [1] "two"

  9. $third
  10. [1] "third"

  11. attr(,"class")
  12. [1] "list"        "Flamboyancy"
  13. >
  14. > GetFirst <- function(x)
  15. + {
  16. +     UseMethod("GetFirst",x)
  17. + }
  18. >
  19. > GetFirst.Flamboyancy <- function(x)
  20. + {
  21. +    return(x$first)
  22. + }
  23. >
  24. > GetFirst(bubba)
  25. [1] "one"
复制代码

使用道具

hifinecon 发表于 2019-1-12 12:15:23 来自手机 |显示全部楼层 |坛友微信交流群
oliyiyi 发表于 2019-1-12 11:37
First, everything in R is treated like as an object. We have seen this with functions. Many of the o ...

使用道具

oliyiyi 发表于 2019-1-12 22:09:22 |显示全部楼层 |坛友微信交流群
In particular, we can use the extract function on stanfit objects to obtain the samples. extract extracts samples from the stanfit object as a list of arrays for parameters of interest, or just an array. In addition, S3 functions as.array, as.matrix, and as.data.frame are defined for stanfit objects (using help("as.array.stanfit") to check out the help document in R).

使用道具

oliyiyi 发表于 2019-4-6 22:57:39 |显示全部楼层 |坛友微信交流群
the class attribute using the class command

使用道具

davil2000 发表于 2019-4-24 10:26:15 |显示全部楼层 |坛友微信交流群
失望!楼主忘记汉语怎么说的?

使用道具

piiroja 发表于 2020-4-14 05:24:27 |显示全部楼层 |坛友微信交流群
thx for sharing~

使用道具

good 09

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-19 01:56