S3 class in R Programming

All things in the R language are considered objects. Objects have attributes and the most common attribute related to an object is class. The command class is used to define a class of an object or learn about the classes of an object.
Class is a vector and this property allows two things:

  • Objects are allowed to inherit from numerous classes
  • Order of inheritance can be specified for complex classes

Example: Checking the class of an object

filter_none

edit
close

play_arrow

link
brightness_4
code

# Creating a vector x consisiting of type of genders
x<-c("female", "male", "male", "female"
  
# Using the command <code>class()</code> 
# to check the class of the vector
class(x)

chevron_right


Output:

[1] "character"

Example: Appending the class of an object

filter_none

edit
close

play_arrow

link
brightness_4
code

# Creating a vector x consisiting of type of genders
x<-c("female", "male", "male", "female"
  
# Using the command <code>class()</code> 
# to append the class of the vector
class(x)<-append(class(x), "Gender")
class(x)

chevron_right


Output:



[1] "character" "Gender"   

Managing Memory

While doing object-oriented programming the programmer can have doubts about which class to use- S3 OR S4?
When comparing both the classes, S4 has a more structured approach while S3 is considered a flexible class.

Memory environments are responsible for flexibility in S3 classes.
An environment is like a local scope and has a variable set associated with it. These variables are accessible if ‘ID’ associated with the environment is known.

To know or set the values of a variable in an environment, commands like assign and get are used.

Example: Assigning and getting values of a variable within the enviroment

filter_none

edit
close

play_arrow

link
brightness_4
code

# Creating a vector x consisiting of type of genders
# Creating a vector for age
age<-c(12, 10, 09)
  
# The command enviroment() can be used 
# to bring the pointer to current enviroment
e <- environment()
e
  
# Setting the value of the variable 
assign("age", 3, e)
ls()
  
# Getting the values of the variable
get("age", e)

chevron_right


Output:

[1] "age" "e"   "x"  
[1] 3   

Environments can be easily created. They can also be embedded in other environments.

Creating an S3 class

An S3 class is the most prevalent and used class in R programming. It is easy to implement this class and most of the predefined classes are of this type.
An S3 object is basically a list with its class attributes assigned some names. And the member variable of the object created is the components of the list.

For creating an S3 object there are two main steps:



  • Create a list(say x) with the required components
  • Then the class can be formed by command class(x) and a name should be assigned to this class

Examples: An S3 object of bank account details can be created easily.

filter_none

edit
close

play_arrow

link
brightness_4
code

x <- list(name ="Arjun", account_no = 1234
          saving = 1500, withdrawn = 234)
class(x)<-"bank"
x

chevron_right


Output: 
$name
[1] "Arjun"

$account_no
[1] 1234

$saving
[1] 1500

$withdrawn
[1] 234

attr(, "class")
[1] "bank"

Examples: An S3 object of a person’s resume can be created easily.

filter_none

edit
close

play_arrow

link
brightness_4
code

x <- list(name ="Arjun", percentage = 95,
          school_name ="ST Xavier")
class(x)<-"resume"
x

chevron_right


Output:

$name
[1] "Arjun"

$percentage
[1] 95

$school_name
[1] "ST Xavier"

attr(, "class")
[1] "resume"

Other languages like- Python, Java, C++, etc have a proper definition for class and the objects have proper defined methods and attributes.
But in the R language in the S3 class system, it is flexible and you can even modify or convert them (object of the same class can be different).

The S3 system in R language consists of three main components

  • Generic function
  • method
  • attributes

Generic Functions

R uses print() function very often. If you type the name of the class, its internals will be printed or you can use the command print(name of the class). But the same function print() is used to print dissimilar things like – vectors, data frames, matrices, etc.

How does the print() recognize this variety of inputs?

The function print() is a generic function and hence is a collection of methods. These methods can further be checked by typing the command methods(print).

filter_none

edit
close

play_arrow

link
brightness_4
code

methods(print)

chevron_right


Output:

 
  [1] print.AES*                                        
  [2] print.Arima*                                      
  [3] print.AsIs                                        
  [4] print.Bibtex*                                     
  [5] print.CRAN_package_reverse_dependencies_and_views*
  [6] print.DLLInfo                                     
  [7] print.DLLInfoList                                 
  [8] print.DLLRegisteredRoutines                       
  [9] print.Date                                        
 [10] print.Dlist                                       
 [11] print.HoltWinters*                                
 [12] print.LaTeX*                                      
 [13] print.Latex*                                      
 [14] print.MethodsFunction*                            
 [15] print.NativeRoutineList                           
 [16] print.PDF_Array*                                  
 [17] print.PDF_Dictionary*                             
 [18] print.PDF_Indirect_Reference*                     
 [19] print.PDF_Keyword*                                
 [20] print.PDF_Name*                                   
 [21] print.PDF_Stream*                                 
 [22] print.PDF_String*                                 
 [23] print.POSIXct                                     
 [24] print.POSIXlt                                     
 [25] print.R6*                                         
 [26] print.R6ClassGenerator*                           
 [27] print.RGBcolorConverter*                          
 [28] print.Rcpp_stack_trace*                           
 [29] print.Rd*                                         
 [30] print.SOCK0node*                                  
 [31] print.SOCKcluster*                                
 [32] print.SOCKnode*                                   
 [33] print.State*                                      
 [34] print.StructTS*                                   
 [35] print.TukeyHSD*                                   
 [36] print.acf*                                        
 [37] print.anova*                                      
 [38] print.aov*                                        
 [39] print.aovlist*                                    
 [40] print.ar*                                         
 [41] print.arima0*                                     
 [42] print.aspell*                                     
 [43] print.aspell_inspect_context*                     
 [44] print.bibentry*                                   
 [45] print.browseVignettes*                            
 [46] print.by                                          
 [47] print.bytes*                                      
 [48] print.changedFiles*                               
 [49] print.checkDocFiles*                              
 [50] print.checkDocStyle*                              
 [51] print.checkFF*                                    
 [52] print.checkRd*                                    
 [53] print.checkReplaceFuns*                           
 [54] print.checkS3methods*                             
 [55] print.checkTnF*                                   
 [56] print.checkVignettes*                             
 [57] print.check_Rd_contents*                          
 [58] print.check_Rd_line_widths*                       
 [59] print.check_Rd_metadata*                          
 [60] print.check_Rd_xrefs*                             
 [61] print.check_RegSym_calls*                         
 [62] print.check_T_and_F*                              
 [63] print.check_code_usage_in_package*                
 [64] print.check_compiled_code*                        
 [65] print.check_demo_index*                           
 [66] print.check_depdef*                               
 [67] print.check_details*                              
 [68] print.check_details_changes*                      
 [69] print.check_doi_db*                               
 [70] print.check_dotInternal*                          
 [71] print.check_make_vars*                            
 [72] print.check_nonAPI_calls*                         
 [73] print.check_package_CRAN_incoming*                
 [74] print.check_package_code_assign_to_globalenv*     
 [75] print.check_package_code_attach*                  
 [76] print.check_package_code_data_into_globalenv*     
 [77] print.check_package_code_startup_functions*       
 [78] print.check_package_code_syntax*                  
 [79] print.check_package_code_unload_functions*        
 [80] print.check_package_compact_datasets*             
 [81] print.check_package_datasets*                     
 [82] print.check_package_depends*                      
 [83] print.check_package_description*                  
 [84] print.check_package_description_encoding*         
 [85] print.check_package_license*                      
 [86] print.check_packages_in_dir*                      
 [87] print.check_packages_used*                        
 [88] print.check_po_files*                             
 [89] print.check_so_symbols*                           
 [90] print.check_url_db*                               
 [91] print.check_vignette_index*                       
 [92] print.citation*                                   
 [93] print.codoc*                                      
 [94] print.codocClasses*                               
 [95] print.codocData*                                  
 [96] print.colorConverter*                             
 [97] print.compactPDF*                                 
 [98] print.condition                                   
 [99] print.connection                                  
[100] print.data.frame                                  
[101] print.default                                     
[102] print.dendrogram*                                 
[103] print.density*                                    
[104] print.difftime                                    
[105] print.dist*                                       
[106] print.dummy_coef*                                 
[107] print.dummy_coef_list*                            
[108] print.ecdf*                                       
[109] print.eigen                                       
[110] print.factanal*                                   
[111] print.factor                                      
[112] print.family*                                     
[113] print.fileSnapshot*                               
[114] print.findLineNumResult*                          
[115] print.formula*                                    
[116] print.fseq*                                       
[117] print.ftable*                                     
[118] print.function                                    
[119] print.getAnywhere*                                
[120] print.glm*                                        
[121] print.hclust*                                     
[122] print.help_files_with_topic*                      
[123] print.hexmode                                     
[124] print.hsearch*                                    
[125] print.hsearch_db*                                 
[126] print.htest*                                      
[127] print.html*                                       
[128] print.html_dependency*                            
[129] print.htmlwidget*                                 
[130] print.infl*                                       
[131] print.integrate*                                  
[132] print.isoreg*                                     
[133] print.kmeans*                                     
[134] print.libraryIQR                                  
[135] print.listof                                      
[136] print.lm*                                         
[137] print.loadings*                                   
[138] print.loess*                                      
[139] print.logLik*                                     
[140] print.ls_str*                                     
[141] print.medpolish*                                  
[142] print.mtable*                                     
[143] print.news_db*                                    
[144] print.nls*                                        
[145] print.noquote                                     
[146] print.numeric_version                             
[147] print.object_size*                                
[148] print.octmode                                     
[149] print.packageDescription*                         
[150] print.packageIQR*                                 
[151] print.packageInfo                                 
[152] print.packageStatus*                              
[153] print.pairwise.htest*                             
[154] print.pdf_doc*                                    
[155] print.pdf_fonts*                                  
[156] print.pdf_info*                                   
[157] print.person*                                     
[158] print.power.htest*                                
[159] print.ppr*                                        
[160] print.prcomp*                                     
[161] print.princomp*                                   
[162] print.proc_time                                   
[163] print.raster*                                     
[164] print.recordedplot*                               
[165] print.restart                                     
[166] print.rle                                         
[167] print.roman*                                      
[168] print.sessionInfo*                                
[169] print.shiny.tag*                                  
[170] print.shiny.tag.list*                             
[171] print.simple.list                                 
[172] print.smooth.spline*                              
[173] print.socket*                                     
[174] print.srcfile                                     
[175] print.srcref                                      
[176] print.stepfun*                                    
[177] print.stl*                                        
[178] print.subdir_tests*                               
[179] print.summarize_CRAN_check_status*                
[180] print.summary.aov*                                
[181] print.summary.aovlist*                            
[182] print.summary.ecdf*                               
[183] print.summary.glm*                                
[184] print.summary.lm*                                 
[185] print.summary.loess*                              
[186] print.summary.manova*                             
[187] print.summary.nls*                                
[188] print.summary.packageStatus*                      
[189] print.summary.ppr*                                
[190] print.summary.prcomp*                             
[191] print.summary.princomp*                           
[192] print.summary.table                               
[193] print.summaryDefault                              
[194] print.suppress_viewer*                            
[195] print.table                                       
[196] print.tables_aov*                                 
[197] print.terms*                                      
[198] print.ts*                                         
[199] print.tskernel*                                   
[200] print.tukeyline*                                  
[201] print.tukeysmooth*                                
[202] print.undoc*                                      
[203] print.vignette*                                   
[204] print.warnings                                    
[205] print.xgettext*                                   
[206] print.xngettext*                                  
[207] print.xtabs*                                      

In the above long list there are important methods like print.factor(). When we print a factor through function print(), the call would automatically dispatch to print.factor()

The class created as – bank, would search for a method named print.bank(), and since no such method exists print.default() is used.
Generic functions have a default method which is used when no match is available.



Creating your own method

Creating your own method is possible.
Now if the class – ‘bank’ searches for print.bank(), it will find this method and use it if we have already created it.

filter_none

edit
close

play_arrow

link
brightness_4
code

x <- list(name ="Arjun", account_no = 1234,
          saving = 1500, withdrawn = 234)
class(x)<-"bank"
print.bank<-function(obj)
{
    cat("Name is ", obj$name, "\n")
    cat(obj$account_no, " is the Acc no of the holder\n ")
    cat(obj$saving, " is the amount of saving in the account \n ")
    cat(obj$withdrawn, " is the withdrawn amount\n")
}
x

chevron_right


Output:

Name is  Arjun 
1234  is the Acc no of the holder
1500  is the amount of saving in the account 
234  is the withdrawn amount

In a general way creating methods can be now easily defined and understood.

  • Firstly define a function(in a generic way) existing out of the class.
  • Secondly, defining the function specifics to a given class.

Based on the class names of an argument to the function and the suffix written in the names of the associated functions, The R environments determine which function to use.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Defining a function
indian <- function(eatslunch = TRUE, myFavorite ="daal")
{
    me <- list(haslunch = eatslunch,
               favoritelunch = myFavorite)
  
    # Set the name for the class
    class(me) <- append(class(me), "indian")
    return(me)
}
  
# Reserving the name of the function and 
# by using the command <code>UseMethod</code>
# R will search for the appropriate function.
setHaslunch <- function(e, newValue)
{
    print("Calling the base setHaslunch function")
    UseMethod("setHaslunch", e)
    print(" this is not executed")
}
   
setHaslunch.default <- function(e, newValue)
{
    print("This objects is unable to be handled.")
    return(e)
}
  
setHaslunch.indian <- function(e, newValue)
{
    print("R is in setHaslunch.indian and is setting the value")
    e$haslunch <- newValue
    return(e)
}
  
# objects calling functions
foodie <- indian()
foodie$haslunch
foodie <- setHaslunch(foodie, FALSE)
foodie$haslunch

chevron_right


Output:

 
[1] TRUE
[1] "Calling the base setHaslunch function"
[1] "R is in setHaslunch.indian and is setting the value"
[1] FALSE 

Attributes

Attributes of an object do not affect the value of an object, but they are a piece of extra information which is used to handle the objects.
The function attributes() can be used to view the attributes of an object.
Examples:An S3 object is created and its attributes are displayed.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Defining a function
x <- list(name ="Arjun", percentage = 95
          school_name ="ST Xavier")
attributes(x)

chevron_right


Output:

 
$names
[1] "name"        "percentage"  "school_name"

$class
[1] "resume"

Also, you can add attributes to an object by using attr.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Defining a function
x <- list(name ="Arju", percentage = 95
          school_name ="ST Xavie")
attr(x, "age")<-c(18)
attributes(x)

chevron_right


Output:

$names
[1] "name"        "percentage"  "school_name"

$age
[1] 18

The S3 has been named so as it originated in the third version of S language. S is a programming language that later modified into R and S plus.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

4


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.