楼主: oliyiyi
235 7

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

版主

大师

86%

还不是VIP/贵宾

-

TA的文库  其他...

计量文库

威望
6
论坛币
628790 个
学术水平
1326 点
热心指数
1434 点
信用等级
1232 点
经验
326683 点
帖子
8793
精华
66
在线时间
4915 小时
注册时间
2007-5-21
最后登录
2019-5-19

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

oliyiyi 发表于 2019-1-12 11:37:17 |显示全部楼层

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   查看全部评分

缺少币币的网友请访问有奖回帖集合
http://bbs.pinggu.org/thread-3990750-1-1.html
stata SPSS
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 |显示全部楼层
失望!楼主忘记汉语怎么说的?
回复

使用道具 举报

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

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

GMT+8, 2019-5-20 03:13