29  Classes and OOP

Object-Oriented Programming (OOP) is a programming paradigm built around objects with associated data, known as attributes, and functions, known as methods.

There are 4 main class systems in R:

S3 and S4 methods are part of generic functions. RC and R6 methods are part of the object, but you can (and should) write generic functions for them as well.

This chapter will focus on the ubiquitous S3 system. For more advanced, and real OOP applications, we recommend looking into the R6 system.

29.1 S3

Most R objects we have been using so far are S3 objects. Data frames are some of the most common S3 objects.

Generic functions are functions that act differently based on the class of the input object. We have already used many of them. For example, summary() works differently on a data.frame, on a factor, or a glm object, etc.

Generic functions in R are saved as functionname.classname() and called automatically, based on the class of the first argument. This allows the same function, e.g. print(), summary(), c(), to have a different effect on objects of different classes. For example, the print() function applied on a data frame, will automatically call print.data.frame(), while applied on a factor, it will call print.factor().
This means that when you type print(iris) this calls print.data.frame(iris).

Note how the R documentation lists usage information separately for each S3 method, e.g. ## S3 method for class 'factor'.

29.1.1 methods()

To get a list of all available methods defined for a specific class,
i.e. “What functions can I use on this object?”

methods(class = "data.frame")
 [1] [             [[            [[<-          [<-           $<-          
 [6] aggregate     anyDuplicated anyNA         as.data.frame as.list      
[11] as.matrix     as.vector     by            cbind         coerce       
[16] dim           dimnames      dimnames<-    droplevels    duplicated   
[21] edit          format        formula       head          initialize   
[26] is.na         Math          merge         na.exclude    na.omit      
[31] Ops           plot          print         prompt        rbind        
[36] row.names     row.names<-   rowsum        show          slotsFromS3  
[41] split         split<-       stack         str           subset       
[46] summary       Summary       t             tail          transform    
[51] type.convert  unique        unstack       within        xtfrm        
see '?methods' for accessing help and source code

Conversely, to get a list of all available methods for a generic function, i.e. “What objects can I use this function on?”

methods(generic.function = "plot")
 [1] plot.acf*           plot.data.frame*    plot.decomposed.ts*
 [4] plot.default        plot.dendrogram*    plot.density*      
 [7] plot.ecdf           plot.factor*        plot.formula*      
[10] plot.function       plot.hclust*        plot.histogram*    
[13] plot.HoltWinters*   plot.isoreg*        plot.lm*           
[16] plot.medpolish*     plot.mlm*           plot.ppr*          
[19] plot.prcomp*        plot.princomp*      plot.profile.nls*  
[22] plot.raster*        plot.spec*          plot.stepfun       
[25] plot.stl*           plot.table*         plot.ts            
[28] plot.tskernel*      plot.TukeyHSD*     
see '?methods' for accessing help and source code

29.1.2 Defining custom S3 classes

It very simple to assign an object to a new class.
There is no formal class definition, an object is directly assigned to a class by name. An object can belong to multiple classes:

x <- rnorm(500)
class(x) <- c("specialvector", "numeric")
class(x)
[1] "specialvector" "numeric"      

The hierarchy of classes goes left to right, meaning that generic methods are searched for classes in the order they appear in the output of class().

If we print x, since there is no print method for class specialvector or for numeric, the default print.default() command is automatically called:

  [1]  1.888719017  0.097097656  0.694888321  0.072377928 -0.497907130
  [6] -0.963478848  1.945085126  0.898447437  0.515390517  2.302865871
 [11] -2.242245260 -1.279759205 -0.583887328 -0.967922802 -1.089577060
 [16]  0.869694816  0.846009016  1.876821940 -1.524111809  1.439032407
 [21] -0.398127484  1.308328220 -0.703791005 -1.442178727  0.500797958
 [26] -1.035910654  0.219342949  1.085511067 -0.407612853  1.677908656
 [31] -0.599957455  1.431962247  0.865893982 -0.918668598 -0.333356254
 [36] -0.506774370 -1.301539247 -0.083430900  0.999780796 -0.452059487
 [41]  2.350073630  0.868283906 -2.919772421 -0.606799133 -0.251386770
 [46]  0.097164944 -0.649528507 -0.250702832  1.170758553 -0.143485168
 [51] -0.649504408 -1.481641736  0.508557423  0.729487808  1.187005929
 [56] -0.540462753  2.602901917 -1.072362853  0.555269006 -0.618126682
 [61] -0.309636782  0.134467835  2.265552843  0.243738962 -0.321611531
 [66] -1.664585522 -1.208026320 -0.223819939 -0.236047059  1.301440515
 [71]  0.618060156  1.105629273 -1.368480636  1.716102126 -0.395680392
 [76] -0.071566694  0.950265872  1.611675820 -1.048867645  0.977930720
 [81] -0.282343475 -1.901756679  1.056641459 -1.106850948  0.556934615
 [86]  0.162788958  0.045349517 -1.075163065 -0.279808683 -0.379805320
 [91] -0.537564718  0.424279639  2.186101018 -1.811633119  1.087184490
 [96]  0.793088008 -0.849708581 -0.361942058 -1.533768847 -1.236061402
[101] -0.270079856  1.187761831  0.340570473 -0.137981531  1.358011032
[106] -0.750176190  0.209266222 -0.227688580 -1.475535856  0.084068631
[111] -0.856426667  0.453994178 -0.452953162 -0.508845374  0.203397047
[116]  0.649616376 -0.752516622 -1.106775612 -0.990907485 -0.396536687
[121]  0.109523288  0.012058814 -0.453786208  0.248090928  3.411106770
[126]  2.924075360  0.631612825  0.351028112 -0.310088990 -2.308941754
[131]  0.016051795  0.140731891  0.508223448 -0.466316935 -0.322001159
[136]  1.274349831  1.742058688 -2.440178231  0.602354967  1.906101154
[141]  0.243900187 -1.159940705 -0.235946047 -1.579460854  0.563077584
[146]  0.590912873  0.432831465  0.464395299  1.595218417 -1.084504898
[151] -0.575994774 -0.212329904  0.561013058  1.513045966 -2.901256662
[156]  0.236783736  0.247433215 -1.104650845 -0.408174668 -0.703644693
[161]  0.825383197  0.043397545  0.011773235  1.246213047  0.329161199
[166] -0.999610336 -1.335542268 -1.956449691  0.862460652 -1.043043477
[171]  0.691613983 -0.427390926  0.984915460  0.139843610 -1.292790293
[176]  0.221488927  0.856045922 -0.049940025  2.017395058  0.550238231
[181] -0.106344015 -0.811813620  1.257597303 -0.140872278 -0.743587100
[186] -0.125836290 -0.311931640  0.415486471 -0.137360537 -0.205817083
[191]  1.866196280  0.650413365 -0.447484092  0.606259902 -0.044509915
[196] -1.061025452  0.212830127 -1.623002913  0.329449139 -1.757995129
[201] -0.190038962  1.305626010 -0.688977648 -0.153763834  0.420199767
[206] -1.140837378 -0.416399540 -0.262825846 -1.126712109 -0.348930869
[211] -0.851698668 -0.631107561 -0.697900522  1.049461405  1.387041666
[216]  0.942548017 -0.233915800 -1.774481705  0.569458580 -2.075914812
[221] -0.136130452 -0.383015062 -0.603570948 -0.264192733 -2.013528057
[226] -0.795864360 -0.799341770 -3.102640487 -0.009318856  1.312464835
[231]  0.410147399  1.302487588  0.671768470 -0.107125669  0.327951678
[236] -1.305653615 -0.120070946  0.569516594 -0.112249452 -1.052867332
[241]  0.734137183  1.182637857 -0.942939423 -0.313163674  0.405513765
[246]  0.338090993 -1.466428602 -1.584924605 -0.455539401 -1.475671715
[251] -0.029305168  0.277414064 -1.585323504 -0.043374523  1.393887795
[256] -0.671366403 -1.058536799 -0.097393476  0.308186911 -0.932942560
[261] -0.001947657  0.551896080  1.080014744  0.210080852 -0.516869808
[266]  0.597855597  1.439218082  2.566123952 -0.139659043 -0.964438341
[271]  0.939401719 -0.599078325 -0.520855987 -0.313126961  0.949284730
[276]  0.530048786 -0.211091113 -0.251527600 -1.327958268  0.463554220
[281]  1.040760627 -0.254002573 -0.872515549 -1.351105024 -1.064915043
[286]  0.834266103 -0.650261791 -0.121939200 -1.531486182 -2.040679939
[291] -0.485136508  1.793857876 -0.100831178  0.197539762  0.164575556
[296] -0.562449174 -1.145785998  1.822383383  0.196696988 -0.515542278
[301] -0.538681511  0.595565954 -1.157056656 -0.927937658 -0.739126324
[306] -1.647481423  0.929855991 -0.662239826  1.794604746  0.039174529
[311] -0.337659923  0.980501620  0.685868568  0.966673868  0.151686399
[316]  0.837460568  0.668583786 -1.095257827  0.569940979 -1.796854551
[321] -1.805234540 -0.680659131  1.718701374  1.119271856  0.256078198
[326]  1.071699022 -1.216394170  0.891814258 -0.393721532 -0.894893115
[331]  0.041732493 -0.525159072 -0.288259827 -0.662641201  1.398287929
[336]  0.381201040 -0.012910825 -0.136455397  1.507650735 -0.890770796
[341]  0.150524004 -1.478292765 -0.953320625  0.007469124 -0.222412081
[346] -0.369027650 -0.340957569  1.045530042 -0.268114901  0.076332385
[351] -0.775765764  0.198095287  3.849825076  0.425579511  0.609997386
[356] -0.043300400  1.907814288  0.028129459  1.907043377  2.518013534
[361] -0.417457530 -0.099604775  1.917888239 -2.465559417 -0.933942683
[366] -0.396749872 -0.297783000 -0.823082006  0.221755793 -0.260121325
[371]  0.770712404 -0.875530693 -1.448220441  0.747984217 -0.713218596
[376]  1.745341879  0.683691640 -0.482598135  0.788042890  0.031669662
[381]  0.488276429  0.225743373 -0.140241509  1.227888418 -0.088444487
[386] -1.682769914  0.107432101  1.557901612 -0.231538521 -0.915386645
[391] -1.669801738  0.587608864 -0.823873532 -0.551904874  0.717795708
[396]  0.479611367  0.875972981 -1.093955320  0.795697414  0.700108129
[401]  0.186424160 -1.267843163  2.252353341  1.112569804  0.877789358
[406]  1.457708491 -1.179410793 -0.047401638 -1.240315693  0.354213302
[411]  0.941336380 -0.694991681  0.411634750 -0.768547015  0.662565419
[416] -0.061829889 -0.812306592 -1.821231939  0.848493949  1.815592263
[421] -1.366648505 -0.926346802 -0.489977313 -0.443625228 -1.202150347
[426]  1.953136087 -1.594888598  0.366225348  1.187669756  0.008506154
[431]  0.450328047  0.024157854 -0.606799600  2.818949809 -0.144273033
[436] -1.769912911  0.542159388 -1.181933147 -1.000300650  1.322710087
[441]  0.016574602 -1.175823772  0.616461479 -0.817208162 -1.144415076
[446]  0.039484307  1.042007036 -0.206650105 -0.594844876 -0.893572906
[451]  0.049787264  1.115586836  0.079503577  0.476245600 -0.045242013
[456] -0.573456802 -0.214194339 -0.151564644  0.036140610 -0.454572067
[461] -0.084356405  0.785101478 -0.198167467  0.047375251 -0.522875850
[466]  0.316202133 -0.109321800 -0.155363965 -0.354693773  0.243650332
[471]  0.605715978  0.531615327  0.131997963 -0.653961268 -1.646040468
[476] -0.346129863  0.545413837  0.367103234  2.876650981 -0.584468699
[481] -0.694200186  1.055826941 -0.231121300  0.207498942 -1.207073774
[486] -0.184573318 -1.253408244 -0.831472224  0.073845745 -1.256317111
[491] -0.293008750  0.702482852 -0.831577335  1.735635518 -0.232380057
[496]  0.796251691  0.293554606 -0.743572118  2.674118971 -0.072190927
attr(,"class")
[1] "specialvector" "numeric"      
  [1]  1.888719017  0.097097656  0.694888321  0.072377928 -0.497907130
  [6] -0.963478848  1.945085126  0.898447437  0.515390517  2.302865871
 [11] -2.242245260 -1.279759205 -0.583887328 -0.967922802 -1.089577060
 [16]  0.869694816  0.846009016  1.876821940 -1.524111809  1.439032407
 [21] -0.398127484  1.308328220 -0.703791005 -1.442178727  0.500797958
 [26] -1.035910654  0.219342949  1.085511067 -0.407612853  1.677908656
 [31] -0.599957455  1.431962247  0.865893982 -0.918668598 -0.333356254
 [36] -0.506774370 -1.301539247 -0.083430900  0.999780796 -0.452059487
 [41]  2.350073630  0.868283906 -2.919772421 -0.606799133 -0.251386770
 [46]  0.097164944 -0.649528507 -0.250702832  1.170758553 -0.143485168
 [51] -0.649504408 -1.481641736  0.508557423  0.729487808  1.187005929
 [56] -0.540462753  2.602901917 -1.072362853  0.555269006 -0.618126682
 [61] -0.309636782  0.134467835  2.265552843  0.243738962 -0.321611531
 [66] -1.664585522 -1.208026320 -0.223819939 -0.236047059  1.301440515
 [71]  0.618060156  1.105629273 -1.368480636  1.716102126 -0.395680392
 [76] -0.071566694  0.950265872  1.611675820 -1.048867645  0.977930720
 [81] -0.282343475 -1.901756679  1.056641459 -1.106850948  0.556934615
 [86]  0.162788958  0.045349517 -1.075163065 -0.279808683 -0.379805320
 [91] -0.537564718  0.424279639  2.186101018 -1.811633119  1.087184490
 [96]  0.793088008 -0.849708581 -0.361942058 -1.533768847 -1.236061402
[101] -0.270079856  1.187761831  0.340570473 -0.137981531  1.358011032
[106] -0.750176190  0.209266222 -0.227688580 -1.475535856  0.084068631
[111] -0.856426667  0.453994178 -0.452953162 -0.508845374  0.203397047
[116]  0.649616376 -0.752516622 -1.106775612 -0.990907485 -0.396536687
[121]  0.109523288  0.012058814 -0.453786208  0.248090928  3.411106770
[126]  2.924075360  0.631612825  0.351028112 -0.310088990 -2.308941754
[131]  0.016051795  0.140731891  0.508223448 -0.466316935 -0.322001159
[136]  1.274349831  1.742058688 -2.440178231  0.602354967  1.906101154
[141]  0.243900187 -1.159940705 -0.235946047 -1.579460854  0.563077584
[146]  0.590912873  0.432831465  0.464395299  1.595218417 -1.084504898
[151] -0.575994774 -0.212329904  0.561013058  1.513045966 -2.901256662
[156]  0.236783736  0.247433215 -1.104650845 -0.408174668 -0.703644693
[161]  0.825383197  0.043397545  0.011773235  1.246213047  0.329161199
[166] -0.999610336 -1.335542268 -1.956449691  0.862460652 -1.043043477
[171]  0.691613983 -0.427390926  0.984915460  0.139843610 -1.292790293
[176]  0.221488927  0.856045922 -0.049940025  2.017395058  0.550238231
[181] -0.106344015 -0.811813620  1.257597303 -0.140872278 -0.743587100
[186] -0.125836290 -0.311931640  0.415486471 -0.137360537 -0.205817083
[191]  1.866196280  0.650413365 -0.447484092  0.606259902 -0.044509915
[196] -1.061025452  0.212830127 -1.623002913  0.329449139 -1.757995129
[201] -0.190038962  1.305626010 -0.688977648 -0.153763834  0.420199767
[206] -1.140837378 -0.416399540 -0.262825846 -1.126712109 -0.348930869
[211] -0.851698668 -0.631107561 -0.697900522  1.049461405  1.387041666
[216]  0.942548017 -0.233915800 -1.774481705  0.569458580 -2.075914812
[221] -0.136130452 -0.383015062 -0.603570948 -0.264192733 -2.013528057
[226] -0.795864360 -0.799341770 -3.102640487 -0.009318856  1.312464835
[231]  0.410147399  1.302487588  0.671768470 -0.107125669  0.327951678
[236] -1.305653615 -0.120070946  0.569516594 -0.112249452 -1.052867332
[241]  0.734137183  1.182637857 -0.942939423 -0.313163674  0.405513765
[246]  0.338090993 -1.466428602 -1.584924605 -0.455539401 -1.475671715
[251] -0.029305168  0.277414064 -1.585323504 -0.043374523  1.393887795
[256] -0.671366403 -1.058536799 -0.097393476  0.308186911 -0.932942560
[261] -0.001947657  0.551896080  1.080014744  0.210080852 -0.516869808
[266]  0.597855597  1.439218082  2.566123952 -0.139659043 -0.964438341
[271]  0.939401719 -0.599078325 -0.520855987 -0.313126961  0.949284730
[276]  0.530048786 -0.211091113 -0.251527600 -1.327958268  0.463554220
[281]  1.040760627 -0.254002573 -0.872515549 -1.351105024 -1.064915043
[286]  0.834266103 -0.650261791 -0.121939200 -1.531486182 -2.040679939
[291] -0.485136508  1.793857876 -0.100831178  0.197539762  0.164575556
[296] -0.562449174 -1.145785998  1.822383383  0.196696988 -0.515542278
[301] -0.538681511  0.595565954 -1.157056656 -0.927937658 -0.739126324
[306] -1.647481423  0.929855991 -0.662239826  1.794604746  0.039174529
[311] -0.337659923  0.980501620  0.685868568  0.966673868  0.151686399
[316]  0.837460568  0.668583786 -1.095257827  0.569940979 -1.796854551
[321] -1.805234540 -0.680659131  1.718701374  1.119271856  0.256078198
[326]  1.071699022 -1.216394170  0.891814258 -0.393721532 -0.894893115
[331]  0.041732493 -0.525159072 -0.288259827 -0.662641201  1.398287929
[336]  0.381201040 -0.012910825 -0.136455397  1.507650735 -0.890770796
[341]  0.150524004 -1.478292765 -0.953320625  0.007469124 -0.222412081
[346] -0.369027650 -0.340957569  1.045530042 -0.268114901  0.076332385
[351] -0.775765764  0.198095287  3.849825076  0.425579511  0.609997386
[356] -0.043300400  1.907814288  0.028129459  1.907043377  2.518013534
[361] -0.417457530 -0.099604775  1.917888239 -2.465559417 -0.933942683
[366] -0.396749872 -0.297783000 -0.823082006  0.221755793 -0.260121325
[371]  0.770712404 -0.875530693 -1.448220441  0.747984217 -0.713218596
[376]  1.745341879  0.683691640 -0.482598135  0.788042890  0.031669662
[381]  0.488276429  0.225743373 -0.140241509  1.227888418 -0.088444487
[386] -1.682769914  0.107432101  1.557901612 -0.231538521 -0.915386645
[391] -1.669801738  0.587608864 -0.823873532 -0.551904874  0.717795708
[396]  0.479611367  0.875972981 -1.093955320  0.795697414  0.700108129
[401]  0.186424160 -1.267843163  2.252353341  1.112569804  0.877789358
[406]  1.457708491 -1.179410793 -0.047401638 -1.240315693  0.354213302
[411]  0.941336380 -0.694991681  0.411634750 -0.768547015  0.662565419
[416] -0.061829889 -0.812306592 -1.821231939  0.848493949  1.815592263
[421] -1.366648505 -0.926346802 -0.489977313 -0.443625228 -1.202150347
[426]  1.953136087 -1.594888598  0.366225348  1.187669756  0.008506154
[431]  0.450328047  0.024157854 -0.606799600  2.818949809 -0.144273033
[436] -1.769912911  0.542159388 -1.181933147 -1.000300650  1.322710087
[441]  0.016574602 -1.175823772  0.616461479 -0.817208162 -1.144415076
[446]  0.039484307  1.042007036 -0.206650105 -0.594844876 -0.893572906
[451]  0.049787264  1.115586836  0.079503577  0.476245600 -0.045242013
[456] -0.573456802 -0.214194339 -0.151564644  0.036140610 -0.454572067
[461] -0.084356405  0.785101478 -0.198167467  0.047375251 -0.522875850
[466]  0.316202133 -0.109321800 -0.155363965 -0.354693773  0.243650332
[471]  0.605715978  0.531615327  0.131997963 -0.653961268 -1.646040468
[476] -0.346129863  0.545413837  0.367103234  2.876650981 -0.584468699
[481] -0.694200186  1.055826941 -0.231121300  0.207498942 -1.207073774
[486] -0.184573318 -1.253408244 -0.831472224  0.073845745 -1.256317111
[491] -0.293008750  0.702482852 -0.831577335  1.735635518 -0.232380057
[496]  0.796251691  0.293554606 -0.743572118  2.674118971 -0.072190927
attr(,"class")
[1] "specialvector" "numeric"      

To create a custom print() function for out new class specialvector, we define a function named print.[classname]:

print.specialvector <- function(x, ...) {
  cat("This is a special vector of length", length(x), "\n")
  cat("Its mean value is", mean(x, na.rm = TRUE), 
      "and its median is", median(x, na.rm = TRUE))
  cat("\nHere are the first few elements:\n", head(x), "\n")
  invisible(x)
}

Now, when you print an object of class specialvector, the custom print() command is invoked:

x
This is a special vector of length 500 
Its mean value is -0.01333819 and its median is -0.09849913
Here are the first few elements:
 1.888719 0.09709766 0.6948883 0.07237793 -0.4979071 -0.9634788 

If needed, you can call the default or another appropriate method directly:

  [1]  1.888719017  0.097097656  0.694888321  0.072377928 -0.497907130
  [6] -0.963478848  1.945085126  0.898447437  0.515390517  2.302865871
 [11] -2.242245260 -1.279759205 -0.583887328 -0.967922802 -1.089577060
 [16]  0.869694816  0.846009016  1.876821940 -1.524111809  1.439032407
 [21] -0.398127484  1.308328220 -0.703791005 -1.442178727  0.500797958
 [26] -1.035910654  0.219342949  1.085511067 -0.407612853  1.677908656
 [31] -0.599957455  1.431962247  0.865893982 -0.918668598 -0.333356254
 [36] -0.506774370 -1.301539247 -0.083430900  0.999780796 -0.452059487
 [41]  2.350073630  0.868283906 -2.919772421 -0.606799133 -0.251386770
 [46]  0.097164944 -0.649528507 -0.250702832  1.170758553 -0.143485168
 [51] -0.649504408 -1.481641736  0.508557423  0.729487808  1.187005929
 [56] -0.540462753  2.602901917 -1.072362853  0.555269006 -0.618126682
 [61] -0.309636782  0.134467835  2.265552843  0.243738962 -0.321611531
 [66] -1.664585522 -1.208026320 -0.223819939 -0.236047059  1.301440515
 [71]  0.618060156  1.105629273 -1.368480636  1.716102126 -0.395680392
 [76] -0.071566694  0.950265872  1.611675820 -1.048867645  0.977930720
 [81] -0.282343475 -1.901756679  1.056641459 -1.106850948  0.556934615
 [86]  0.162788958  0.045349517 -1.075163065 -0.279808683 -0.379805320
 [91] -0.537564718  0.424279639  2.186101018 -1.811633119  1.087184490
 [96]  0.793088008 -0.849708581 -0.361942058 -1.533768847 -1.236061402
[101] -0.270079856  1.187761831  0.340570473 -0.137981531  1.358011032
[106] -0.750176190  0.209266222 -0.227688580 -1.475535856  0.084068631
[111] -0.856426667  0.453994178 -0.452953162 -0.508845374  0.203397047
[116]  0.649616376 -0.752516622 -1.106775612 -0.990907485 -0.396536687
[121]  0.109523288  0.012058814 -0.453786208  0.248090928  3.411106770
[126]  2.924075360  0.631612825  0.351028112 -0.310088990 -2.308941754
[131]  0.016051795  0.140731891  0.508223448 -0.466316935 -0.322001159
[136]  1.274349831  1.742058688 -2.440178231  0.602354967  1.906101154
[141]  0.243900187 -1.159940705 -0.235946047 -1.579460854  0.563077584
[146]  0.590912873  0.432831465  0.464395299  1.595218417 -1.084504898
[151] -0.575994774 -0.212329904  0.561013058  1.513045966 -2.901256662
[156]  0.236783736  0.247433215 -1.104650845 -0.408174668 -0.703644693
[161]  0.825383197  0.043397545  0.011773235  1.246213047  0.329161199
[166] -0.999610336 -1.335542268 -1.956449691  0.862460652 -1.043043477
[171]  0.691613983 -0.427390926  0.984915460  0.139843610 -1.292790293
[176]  0.221488927  0.856045922 -0.049940025  2.017395058  0.550238231
[181] -0.106344015 -0.811813620  1.257597303 -0.140872278 -0.743587100
[186] -0.125836290 -0.311931640  0.415486471 -0.137360537 -0.205817083
[191]  1.866196280  0.650413365 -0.447484092  0.606259902 -0.044509915
[196] -1.061025452  0.212830127 -1.623002913  0.329449139 -1.757995129
[201] -0.190038962  1.305626010 -0.688977648 -0.153763834  0.420199767
[206] -1.140837378 -0.416399540 -0.262825846 -1.126712109 -0.348930869
[211] -0.851698668 -0.631107561 -0.697900522  1.049461405  1.387041666
[216]  0.942548017 -0.233915800 -1.774481705  0.569458580 -2.075914812
[221] -0.136130452 -0.383015062 -0.603570948 -0.264192733 -2.013528057
[226] -0.795864360 -0.799341770 -3.102640487 -0.009318856  1.312464835
[231]  0.410147399  1.302487588  0.671768470 -0.107125669  0.327951678
[236] -1.305653615 -0.120070946  0.569516594 -0.112249452 -1.052867332
[241]  0.734137183  1.182637857 -0.942939423 -0.313163674  0.405513765
[246]  0.338090993 -1.466428602 -1.584924605 -0.455539401 -1.475671715
[251] -0.029305168  0.277414064 -1.585323504 -0.043374523  1.393887795
[256] -0.671366403 -1.058536799 -0.097393476  0.308186911 -0.932942560
[261] -0.001947657  0.551896080  1.080014744  0.210080852 -0.516869808
[266]  0.597855597  1.439218082  2.566123952 -0.139659043 -0.964438341
[271]  0.939401719 -0.599078325 -0.520855987 -0.313126961  0.949284730
[276]  0.530048786 -0.211091113 -0.251527600 -1.327958268  0.463554220
[281]  1.040760627 -0.254002573 -0.872515549 -1.351105024 -1.064915043
[286]  0.834266103 -0.650261791 -0.121939200 -1.531486182 -2.040679939
[291] -0.485136508  1.793857876 -0.100831178  0.197539762  0.164575556
[296] -0.562449174 -1.145785998  1.822383383  0.196696988 -0.515542278
[301] -0.538681511  0.595565954 -1.157056656 -0.927937658 -0.739126324
[306] -1.647481423  0.929855991 -0.662239826  1.794604746  0.039174529
[311] -0.337659923  0.980501620  0.685868568  0.966673868  0.151686399
[316]  0.837460568  0.668583786 -1.095257827  0.569940979 -1.796854551
[321] -1.805234540 -0.680659131  1.718701374  1.119271856  0.256078198
[326]  1.071699022 -1.216394170  0.891814258 -0.393721532 -0.894893115
[331]  0.041732493 -0.525159072 -0.288259827 -0.662641201  1.398287929
[336]  0.381201040 -0.012910825 -0.136455397  1.507650735 -0.890770796
[341]  0.150524004 -1.478292765 -0.953320625  0.007469124 -0.222412081
[346] -0.369027650 -0.340957569  1.045530042 -0.268114901  0.076332385
[351] -0.775765764  0.198095287  3.849825076  0.425579511  0.609997386
[356] -0.043300400  1.907814288  0.028129459  1.907043377  2.518013534
[361] -0.417457530 -0.099604775  1.917888239 -2.465559417 -0.933942683
[366] -0.396749872 -0.297783000 -0.823082006  0.221755793 -0.260121325
[371]  0.770712404 -0.875530693 -1.448220441  0.747984217 -0.713218596
[376]  1.745341879  0.683691640 -0.482598135  0.788042890  0.031669662
[381]  0.488276429  0.225743373 -0.140241509  1.227888418 -0.088444487
[386] -1.682769914  0.107432101  1.557901612 -0.231538521 -0.915386645
[391] -1.669801738  0.587608864 -0.823873532 -0.551904874  0.717795708
[396]  0.479611367  0.875972981 -1.093955320  0.795697414  0.700108129
[401]  0.186424160 -1.267843163  2.252353341  1.112569804  0.877789358
[406]  1.457708491 -1.179410793 -0.047401638 -1.240315693  0.354213302
[411]  0.941336380 -0.694991681  0.411634750 -0.768547015  0.662565419
[416] -0.061829889 -0.812306592 -1.821231939  0.848493949  1.815592263
[421] -1.366648505 -0.926346802 -0.489977313 -0.443625228 -1.202150347
[426]  1.953136087 -1.594888598  0.366225348  1.187669756  0.008506154
[431]  0.450328047  0.024157854 -0.606799600  2.818949809 -0.144273033
[436] -1.769912911  0.542159388 -1.181933147 -1.000300650  1.322710087
[441]  0.016574602 -1.175823772  0.616461479 -0.817208162 -1.144415076
[446]  0.039484307  1.042007036 -0.206650105 -0.594844876 -0.893572906
[451]  0.049787264  1.115586836  0.079503577  0.476245600 -0.045242013
[456] -0.573456802 -0.214194339 -0.151564644  0.036140610 -0.454572067
[461] -0.084356405  0.785101478 -0.198167467  0.047375251 -0.522875850
[466]  0.316202133 -0.109321800 -0.155363965 -0.354693773  0.243650332
[471]  0.605715978  0.531615327  0.131997963 -0.653961268 -1.646040468
[476] -0.346129863  0.545413837  0.367103234  2.876650981 -0.584468699
[481] -0.694200186  1.055826941 -0.231121300  0.207498942 -1.207073774
[486] -0.184573318 -1.253408244 -0.831472224  0.073845745 -1.256317111
[491] -0.293008750  0.702482852 -0.831577335  1.735635518 -0.232380057
[496]  0.796251691  0.293554606 -0.743572118  2.674118971 -0.072190927
attr(,"class")
[1] "specialvector" "numeric"      

You can change the vector back to a regular numeric vector, or a different class, just as easily:

class(x) <- "numeric"
x
  [1]  1.888719017  0.097097656  0.694888321  0.072377928 -0.497907130
  [6] -0.963478848  1.945085126  0.898447437  0.515390517  2.302865871
 [11] -2.242245260 -1.279759205 -0.583887328 -0.967922802 -1.089577060
 [16]  0.869694816  0.846009016  1.876821940 -1.524111809  1.439032407
 [21] -0.398127484  1.308328220 -0.703791005 -1.442178727  0.500797958
 [26] -1.035910654  0.219342949  1.085511067 -0.407612853  1.677908656
 [31] -0.599957455  1.431962247  0.865893982 -0.918668598 -0.333356254
 [36] -0.506774370 -1.301539247 -0.083430900  0.999780796 -0.452059487
 [41]  2.350073630  0.868283906 -2.919772421 -0.606799133 -0.251386770
 [46]  0.097164944 -0.649528507 -0.250702832  1.170758553 -0.143485168
 [51] -0.649504408 -1.481641736  0.508557423  0.729487808  1.187005929
 [56] -0.540462753  2.602901917 -1.072362853  0.555269006 -0.618126682
 [61] -0.309636782  0.134467835  2.265552843  0.243738962 -0.321611531
 [66] -1.664585522 -1.208026320 -0.223819939 -0.236047059  1.301440515
 [71]  0.618060156  1.105629273 -1.368480636  1.716102126 -0.395680392
 [76] -0.071566694  0.950265872  1.611675820 -1.048867645  0.977930720
 [81] -0.282343475 -1.901756679  1.056641459 -1.106850948  0.556934615
 [86]  0.162788958  0.045349517 -1.075163065 -0.279808683 -0.379805320
 [91] -0.537564718  0.424279639  2.186101018 -1.811633119  1.087184490
 [96]  0.793088008 -0.849708581 -0.361942058 -1.533768847 -1.236061402
[101] -0.270079856  1.187761831  0.340570473 -0.137981531  1.358011032
[106] -0.750176190  0.209266222 -0.227688580 -1.475535856  0.084068631
[111] -0.856426667  0.453994178 -0.452953162 -0.508845374  0.203397047
[116]  0.649616376 -0.752516622 -1.106775612 -0.990907485 -0.396536687
[121]  0.109523288  0.012058814 -0.453786208  0.248090928  3.411106770
[126]  2.924075360  0.631612825  0.351028112 -0.310088990 -2.308941754
[131]  0.016051795  0.140731891  0.508223448 -0.466316935 -0.322001159
[136]  1.274349831  1.742058688 -2.440178231  0.602354967  1.906101154
[141]  0.243900187 -1.159940705 -0.235946047 -1.579460854  0.563077584
[146]  0.590912873  0.432831465  0.464395299  1.595218417 -1.084504898
[151] -0.575994774 -0.212329904  0.561013058  1.513045966 -2.901256662
[156]  0.236783736  0.247433215 -1.104650845 -0.408174668 -0.703644693
[161]  0.825383197  0.043397545  0.011773235  1.246213047  0.329161199
[166] -0.999610336 -1.335542268 -1.956449691  0.862460652 -1.043043477
[171]  0.691613983 -0.427390926  0.984915460  0.139843610 -1.292790293
[176]  0.221488927  0.856045922 -0.049940025  2.017395058  0.550238231
[181] -0.106344015 -0.811813620  1.257597303 -0.140872278 -0.743587100
[186] -0.125836290 -0.311931640  0.415486471 -0.137360537 -0.205817083
[191]  1.866196280  0.650413365 -0.447484092  0.606259902 -0.044509915
[196] -1.061025452  0.212830127 -1.623002913  0.329449139 -1.757995129
[201] -0.190038962  1.305626010 -0.688977648 -0.153763834  0.420199767
[206] -1.140837378 -0.416399540 -0.262825846 -1.126712109 -0.348930869
[211] -0.851698668 -0.631107561 -0.697900522  1.049461405  1.387041666
[216]  0.942548017 -0.233915800 -1.774481705  0.569458580 -2.075914812
[221] -0.136130452 -0.383015062 -0.603570948 -0.264192733 -2.013528057
[226] -0.795864360 -0.799341770 -3.102640487 -0.009318856  1.312464835
[231]  0.410147399  1.302487588  0.671768470 -0.107125669  0.327951678
[236] -1.305653615 -0.120070946  0.569516594 -0.112249452 -1.052867332
[241]  0.734137183  1.182637857 -0.942939423 -0.313163674  0.405513765
[246]  0.338090993 -1.466428602 -1.584924605 -0.455539401 -1.475671715
[251] -0.029305168  0.277414064 -1.585323504 -0.043374523  1.393887795
[256] -0.671366403 -1.058536799 -0.097393476  0.308186911 -0.932942560
[261] -0.001947657  0.551896080  1.080014744  0.210080852 -0.516869808
[266]  0.597855597  1.439218082  2.566123952 -0.139659043 -0.964438341
[271]  0.939401719 -0.599078325 -0.520855987 -0.313126961  0.949284730
[276]  0.530048786 -0.211091113 -0.251527600 -1.327958268  0.463554220
[281]  1.040760627 -0.254002573 -0.872515549 -1.351105024 -1.064915043
[286]  0.834266103 -0.650261791 -0.121939200 -1.531486182 -2.040679939
[291] -0.485136508  1.793857876 -0.100831178  0.197539762  0.164575556
[296] -0.562449174 -1.145785998  1.822383383  0.196696988 -0.515542278
[301] -0.538681511  0.595565954 -1.157056656 -0.927937658 -0.739126324
[306] -1.647481423  0.929855991 -0.662239826  1.794604746  0.039174529
[311] -0.337659923  0.980501620  0.685868568  0.966673868  0.151686399
[316]  0.837460568  0.668583786 -1.095257827  0.569940979 -1.796854551
[321] -1.805234540 -0.680659131  1.718701374  1.119271856  0.256078198
[326]  1.071699022 -1.216394170  0.891814258 -0.393721532 -0.894893115
[331]  0.041732493 -0.525159072 -0.288259827 -0.662641201  1.398287929
[336]  0.381201040 -0.012910825 -0.136455397  1.507650735 -0.890770796
[341]  0.150524004 -1.478292765 -0.953320625  0.007469124 -0.222412081
[346] -0.369027650 -0.340957569  1.045530042 -0.268114901  0.076332385
[351] -0.775765764  0.198095287  3.849825076  0.425579511  0.609997386
[356] -0.043300400  1.907814288  0.028129459  1.907043377  2.518013534
[361] -0.417457530 -0.099604775  1.917888239 -2.465559417 -0.933942683
[366] -0.396749872 -0.297783000 -0.823082006  0.221755793 -0.260121325
[371]  0.770712404 -0.875530693 -1.448220441  0.747984217 -0.713218596
[376]  1.745341879  0.683691640 -0.482598135  0.788042890  0.031669662
[381]  0.488276429  0.225743373 -0.140241509  1.227888418 -0.088444487
[386] -1.682769914  0.107432101  1.557901612 -0.231538521 -0.915386645
[391] -1.669801738  0.587608864 -0.823873532 -0.551904874  0.717795708
[396]  0.479611367  0.875972981 -1.093955320  0.795697414  0.700108129
[401]  0.186424160 -1.267843163  2.252353341  1.112569804  0.877789358
[406]  1.457708491 -1.179410793 -0.047401638 -1.240315693  0.354213302
[411]  0.941336380 -0.694991681  0.411634750 -0.768547015  0.662565419
[416] -0.061829889 -0.812306592 -1.821231939  0.848493949  1.815592263
[421] -1.366648505 -0.926346802 -0.489977313 -0.443625228 -1.202150347
[426]  1.953136087 -1.594888598  0.366225348  1.187669756  0.008506154
[431]  0.450328047  0.024157854 -0.606799600  2.818949809 -0.144273033
[436] -1.769912911  0.542159388 -1.181933147 -1.000300650  1.322710087
[441]  0.016574602 -1.175823772  0.616461479 -0.817208162 -1.144415076
[446]  0.039484307  1.042007036 -0.206650105 -0.594844876 -0.893572906
[451]  0.049787264  1.115586836  0.079503577  0.476245600 -0.045242013
[456] -0.573456802 -0.214194339 -0.151564644  0.036140610 -0.454572067
[461] -0.084356405  0.785101478 -0.198167467  0.047375251 -0.522875850
[466]  0.316202133 -0.109321800 -0.155363965 -0.354693773  0.243650332
[471]  0.605715978  0.531615327  0.131997963 -0.653961268 -1.646040468
[476] -0.346129863  0.545413837  0.367103234  2.876650981 -0.584468699
[481] -0.694200186  1.055826941 -0.231121300  0.207498942 -1.207073774
[486] -0.184573318 -1.253408244 -0.831472224  0.073845745 -1.256317111
[491] -0.293008750  0.702482852 -0.831577335  1.735635518 -0.232380057
[496]  0.796251691  0.293554606 -0.743572118  2.674118971 -0.072190927