楼主: oliyiyi
2091 9

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

版主

已卖:2996份资源

泰斗

1%

还不是VIP/贵宾

-

TA的文库  其他...

计量文库

威望
7
论坛币
47275 个
通用积分
31671.2767
学术水平
1454 点
热心指数
1573 点
信用等级
1364 点
经验
384134 点
帖子
9629
精华
66
在线时间
5508 小时
注册时间
2007-5-21
最后登录
2025-7-8

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

楼主
oliyiyi 发表于 2019-1-12 11:37:17 |AI写论文

+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).

7
oliyiyi 发表于 2019-4-6 22:57:39
the class attribute using the class command

8
davil2000 发表于 2019-4-24 10:26:15
失望!楼主忘记汉语怎么说的?

9
piiroja 发表于 2020-4-14 05:24:27
thx for sharing~

10
wahahapinggu456 发表于 2020-6-14 19:58:05
good 09

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

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-15 04:50