How to create an S4 object in R?

R ProgrammingServer Side ProgrammingProgramming

To create an S4 object, we can use setClass function where we will pass the object name, column names, and the type of the data that will be stored in the columns. For example, if we want to create an S4 with name data and two numerical columns called by x and y then we can use setClass("data",representation(x1="numeric",x2="numeric")).

Example1

> setClass("data1",representation(x1="numeric",x2="numeric"))
> data1<-new("data1",x1=rnorm(20),x2=rexp(20,1.12))
> data1

Output

An object of class "data1"
Slot "x1":
[1] -0.586187627 0.853689097 -0.602612795 -2.194235741 -1.318522292
[6] -0.984882420 0.273584140 0.364691611 1.025472248 1.198547297
[11] -0.709282551 -0.001441127 -0.201348012 1.296811172 1.520093861
[16] 2.071031215 0.472877022 0.616211695 0.642165615 -0.122773000

Slot "x2":
[1] 0.38902289 0.20631450 0.02105516 0.24891420 2.37347874 0.43704064
[7] 0.79887672 1.95711822 0.69214407 1.17875759 0.10490338 0.69417206
[13] 0.60324447 0.03573967 0.27204874 1.63015638 1.94575940 2.97829841
[19] 0.22643380 2.06821215

Example2

> setClass("data2",representation(x1="integer",x2="numeric"))
> data2<-new("data2",x1=rpois(200,5),x2=rexp(50,1.12))
> data2

Output

An object of class "data2"
Slot "x1":
[1] 3 7 4 8 5 7 5 11 6 4 5 3 2 7 5 5 4 3 7 8 12 6 10 6 3
[26] 7 7 6 4 2 6 6 8 7 8 8 5 2 3 4 7 2 4 1 3 4 7 4 10 5
[51] 7 2 4 3 8 6 4 4 6 7 8 4 5 5 3 4 2 7 7 6 1 6 3 5 2
[76] 5 6 7 3 7 5 7 5 8 2 4 4 2 3 6 1 6 5 5 3 4 3 8 5 7
[101] 4 3 8 2 6 3 3 5 1 2 4 6 4 6 2 4 4 4 4 9 4 4 7 2 9
[126] 4 3 4 3 4 7 5 5 2 2 6 4 6 5 5 6 8 4 7 6 3 7 7 7 8
[151] 8 6 4 7 4 4 3 10 4 6 2 5 5 4 4 6 7 5 7 0 6 8 5 8 9
[176] 3 5 5 4 8 4 4 6 5 7 9 6 2 2 2 5 9 3 5 3 3 4 6 2 6

Slot "x2":
[1] 0.03141964 0.49307236 0.31423727 0.43521757 0.52619093 0.70795201
[7] 0.35462825 0.59378101 0.10527933 0.70027538 0.44882733 0.43956142
[13] 0.09664605 0.50706106 1.65260142 0.36428909 0.61297587 1.01703946
[19] 0.89316946 0.59825470 1.32223944 1.77853473 0.19214180 4.76283291
[25] 0.51096582 1.07728540 0.94746461 1.03008930 0.80508219 2.91018171
[31] 0.13807893 0.98123535 0.71989867 1.32550897 0.86492233 0.06968105
[37] 0.75559512 0.27958713 0.18840316 1.39449247 3.78111847 0.26038046
[43] 0.02072275 0.81411699 0.89175522 0.13439256 1.16051005 1.00565524
[49] 0.44863428 0.59886756

Example3

> setClass("data3",representation(x1="character",x2="numeric"))
> data3<-new("data3",x1=sample(LETTERS[1:4],50,replace=TRUE),x2=rexp(50,1.12))
> data3

Output

An object of class "data3"
Slot "x1":
[1] "C" "D" "A" "C" "D" "D" "C" "D" "C" "A" "A" "B" "C" "D" "C" "D" "C" "A" "D"
[20] "C" "C" "A" "B" "B" "C" "D" "D" "B" "B" "C" "A" "C" "D" "A" "C" "D" "A" "C"
[39] "C" "C" "B" "C" "B" "B" "D" "C" "A" "C" "A" "A"

Slot "x2":
[1] 0.15262639 0.18257750 0.66531800 0.90077904 0.31199878 0.15326597
[7] 0.14915567 0.09891334 1.91290294 1.64658850 0.17738544 0.07428495
[13] 0.51221999 1.19112341 0.16764472 1.29586175 0.67945778 0.33704154
[19] 0.21145555 0.28791368 0.95651553 0.48383674 0.76274501 0.71038690
[25] 1.34688895 1.77748828 0.63969314 0.29701294 0.04734766 1.02116237
[31] 0.27368908 0.04268661 0.77449047 3.70772112 0.40526753 0.06333750
[37] 0.26435011 1.03701168 0.08280528 0.86331936 0.15271265 1.45303032
[43] 0.04458336 0.54749522 0.44025731 0.20837975 0.21421977 0.16732185
[49] 1.46172264 0.70931165

Example4

> setClass("data4",representation(x1="logical",x2="numeric"))
> data4<-new("data4",x1=sample(as.logical(c(0,1)),50,replace=TRUE),x2=rnorm(50,1,0.50))
> data4

Output

An object of class "data4"
Slot "x1":
[1] FALSE TRUE FALSE TRUE TRUE FALSE TRUE TRUE FALSE FALSE TRUE TRUE
[13] TRUE TRUE FALSE FALSE TRUE TRUE TRUE FALSE TRUE FALSE TRUE FALSE
[25] TRUE TRUE TRUE TRUE TRUE TRUE FALSE TRUE FALSE TRUE FALSE TRUE
[37] TRUE FALSE FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE
[49] TRUE FALSE

Slot "x2":
[1] 1.4535492 0.8230134 0.9926188 0.9236218 0.9568131 1.2355998
[7] -0.2649343 1.4839302 0.6435250 0.8384010 1.4399601 1.3696312
[13] 0.2847440 0.6539318 1.2568808 1.4457016 1.1884043 1.3024577
[19] 1.5923689 1.2796569 0.9942924 0.6104080 0.4510600 0.9901056
[25] 0.9496257 1.1278555 0.5048898 1.0492706 1.5142966 0.8459955
[31] 1.4398791 1.0121801 0.9473674 0.2266796 1.3360711 0.2354370
[37] 0.4838408 1.4131759 0.1566150 1.4218652 1.1542315 2.0074517
[43] 1.0019310 0.3909861 0.6707586 0.9373494 1.4065083 0.1781948
[49] 1.4708116 1.1577926
raja
Published on 21-Nov-2020 05:56:32
Advertisements