Preliminary notions

Básic concepts such as object, names, types, and class.

R.Andres Castaneda
2022-11-22

Slides

Hands on

Exercises

  1. What is the difference between c(list(1:3), list(9)) and list(list(1:3), list(9))

  2. Is there any difference between these three ways of assignment?

assign('a', c(10, 150, 30, 45, 20.3))
a <- c(10, 150, 30, 45, 20.3)
a = c(10, 150, 30, 45, 20.3)
  1. The dim() function gets the dimension of an object. Why dim(a) is NULL?

  2. Why is.character(c('blue',10,'green',20)) is TRUE?

R comes with a variety of datasets to work with (Similar to sysuse auto in Stata). You can see all the datasets available by typing library(help = "datasets").

  1. In a tibble bound to the name tb, add a column named x with the vector rivers from the internal datasets.

Understanding factors

From characters to factors

## Simple character vector
degree <- c("master", "master", "bachelor", "phd", "bachelor", "master")
degree
[1] "master"   "master"   "bachelor" "phd"      "bachelor" "master"  
class(degree)
[1] "character"
typeof(degree)
[1] "character"
attributes(degree)
NULL
summary(degree)
   Length     Class      Mode 
        6 character character 
degree_fc <- factor(degree, levels = c("bachelor", "master",  "phd"))
class(degree_fc)
[1] "factor"
typeof(degree_fc)
[1] "integer"
attributes(degree_fc)
$levels
[1] "bachelor" "master"   "phd"     

$class
[1] "factor"
summary(degree_fc)
bachelor   master      phd 
       2        3        1 
plot(degree_fc, col = "steelblue")

plot(degree, col = "steelblue")
Error in plot.window(...): need finite 'ylim' values

From integer to factors

degree2    <- c(1L, 3L, 1L)
class(degree2)
[1] "integer"
typeof(degree2)
[1] "integer"
attributes(degree2)
NULL
summary(degree2)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 1.0000  1.0000  1.0000  1.6667  2.0000  3.0000 
degree2_fc <- factor(degree2,
                    levels = c(1, 2, 3),
                    labels = c("bachelor", "master", "phd"))
class(degree2_fc)
[1] "factor"
typeof(degree2_fc)
[1] "integer"
attributes(degree2_fc)
$levels
[1] "bachelor" "master"   "phd"     

$class
[1] "factor"
summary(degree2_fc)
bachelor   master      phd 
       2        0        1 
plot(degree2)
plot(degree2_fc)

The beauty of classes

methods("summary")
 [1] summary.aov                        
 [2] summary.aovlist*                   
 [3] summary.aspell*                    
 [4] summary.check_packages_in_dir*     
 [5] summary.connection                 
 [6] summary.data.frame                 
 [7] summary.Date                       
 [8] summary.default                    
 [9] summary.ecdf*                      
[10] summary.factor                     
[11] summary.glm                        
[12] summary.infl*                      
[13] summary.lm                         
[14] summary.loess*                     
[15] summary.manova                     
[16] summary.matrix                     
[17] summary.mlm*                       
[18] summary.nls*                       
[19] summary.packageStatus*             
[20] summary.POSIXct                    
[21] summary.POSIXlt                    
[22] summary.ppr*                       
[23] summary.prcomp*                    
[24] summary.princomp*                  
[25] summary.proc_time                  
[26] summary.rlang:::list_of_conditions*
[27] summary.rlang_error*               
[28] summary.rlang_message*             
[29] summary.rlang_trace*               
[30] summary.rlang_warning*             
[31] summary.srcfile                    
[32] summary.srcref                     
[33] summary.stepfun                    
[34] summary.stl*                       
[35] summary.table                      
[36] summary.tukeysmooth*               
[37] summary.vctrs_sclr*                
[38] summary.vctrs_vctr*                
[39] summary.warnings                   
see '?methods' for accessing help and source code
methods("print")
  [1] print.acf*                                          
  [2] print.AES*                                          
  [3] print.anova*                                        
  [4] print.aov*                                          
  [5] print.aovlist*                                      
  [6] print.ar*                                           
  [7] print.Arima*                                        
  [8] print.arima0*                                       
  [9] print.AsIs                                          
 [10] print.aspell*                                       
 [11] print.aspell_inspect_context*                       
 [12] print.bibentry*                                     
 [13] print.Bibtex*                                       
 [14] print.browseVignettes*                              
 [15] print.bslib_fragment*                               
 [16] print.bslib_page*                                   
 [17] print.by                                            
 [18] print.cachem*                                       
 [19] print.changedFiles*                                 
 [20] print.check_bogus_return*                           
 [21] print.check_code_usage_in_package*                  
 [22] print.check_compiled_code*                          
 [23] print.check_demo_index*                             
 [24] print.check_depdef*                                 
 [25] print.check_details*                                
 [26] print.check_details_changes*                        
 [27] print.check_doi_db*                                 
 [28] print.check_dotInternal*                            
 [29] print.check_make_vars*                              
 [30] print.check_nonAPI_calls*                           
 [31] print.check_package_code_assign_to_globalenv*       
 [32] print.check_package_code_attach*                    
 [33] print.check_package_code_data_into_globalenv*       
 [34] print.check_package_code_startup_functions*         
 [35] print.check_package_code_syntax*                    
 [36] print.check_package_code_unload_functions*          
 [37] print.check_package_compact_datasets*               
 [38] print.check_package_CRAN_incoming*                  
 [39] print.check_package_datalist*                       
 [40] print.check_package_datasets*                       
 [41] print.check_package_depends*                        
 [42] print.check_package_description*                    
 [43] print.check_package_description_encoding*           
 [44] print.check_package_license*                        
 [45] print.check_packages_in_dir*                        
 [46] print.check_packages_used*                          
 [47] print.check_po_files*                               
 [48] print.check_pragmas*                                
 [49] print.check_Rd_line_widths*                         
 [50] print.check_Rd_metadata*                            
 [51] print.check_Rd_xrefs*                               
 [52] print.check_RegSym_calls*                           
 [53] print.check_S3_methods_needing_delayed_registration*
 [54] print.check_so_symbols*                             
 [55] print.check_T_and_F*                                
 [56] print.check_url_db*                                 
 [57] print.check_vignette_index*                         
 [58] print.checkDocFiles*                                
 [59] print.checkDocStyle*                                
 [60] print.checkFF*                                      
 [61] print.checkRd*                                      
 [62] print.checkRdContents*                              
 [63] print.checkReplaceFuns*                             
 [64] print.checkS3methods*                               
 [65] print.checkTnF*                                     
 [66] print.checkVignettes*                               
 [67] print.citation*                                     
 [68] print.cli_ansi_html_style*                          
 [69] print.cli_ansi_string*                              
 [70] print.cli_ansi_style*                               
 [71] print.cli_boxx*                                     
 [72] print.cli_diff_chr*                                 
 [73] print.cli_doc*                                      
 [74] print.cli_progress_demo*                            
 [75] print.cli_rule*                                     
 [76] print.cli_sitrep*                                   
 [77] print.cli_spark*                                    
 [78] print.cli_spinner*                                  
 [79] print.cli_tree*                                     
 [80] print.codoc*                                        
 [81] print.codocClasses*                                 
 [82] print.codocData*                                    
 [83] print.colorConverter*                               
 [84] print.compactPDF*                                   
 [85] print.condition                                     
 [86] print.connection                                    
 [87] print.CRAN_package_reverse_dependencies_and_views*  
 [88] print.css*                                          
 [89] print.data.frame                                    
 [90] print.Date                                          
 [91] print.default                                       
 [92] print.dendrogram*                                   
 [93] print.density*                                      
 [94] print.difftime                                      
 [95] print.dist*                                         
 [96] print.Dlist                                         
 [97] print.DLLInfo                                       
 [98] print.DLLInfoList                                   
 [99] print.DLLRegisteredRoutines                         
[100] print.document_context*                             
[101] print.document_position*                            
[102] print.document_range*                               
[103] print.document_selection*                           
[104] print.dummy_coef*                                   
[105] print.dummy_coef_list*                              
[106] print.ecdf*                                         
[107] print.eigen                                         
[108] print.factanal*                                     
[109] print.factor                                        
[110] print.family*                                       
[111] print.fileSnapshot*                                 
[112] print.findLineNumResult*                            
[113] print.formula*                                      
[114] print.fs_bytes*                                     
[115] print.fs_path*                                      
[116] print.fs_perms*                                     
[117] print.fseq*                                         
[118] print.ftable*                                       
[119] print.function                                      
[120] print.getAnywhere*                                  
[121] print.glm*                                          
[122] print.hclust*                                       
[123] print.help_files_with_topic*                        
[124] print.hexmode                                       
[125] print.HoltWinters*                                  
[126] print.hsearch*                                      
[127] print.hsearch_db*                                   
[128] print.htest*                                        
[129] print.html*                                         
[130] print.html_dependency*                              
[131] print.htmltools.selector*                           
[132] print.htmltools.selector.list*                      
[133] print.infl*                                         
[134] print.integrate*                                    
[135] print.isoreg*                                       
[136] print.json*                                         
[137] print.key_missing*                                  
[138] print.kmeans*                                       
[139] print.knitr_kable*                                  
[140] print.Latex*                                        
[141] print.LaTeX*                                        
[142] print.libraryIQR                                    
[143] print.listof                                        
[144] print.lm*                                           
[145] print.loadings*                                     
[146] print.loess*                                        
[147] print.logLik*                                       
[148] print.ls_str*                                       
[149] print.medpolish*                                    
[150] print.memoised*                                     
[151] print.MethodsFunction*                              
[152] print.mtable*                                       
[153] print.NativeRoutineList                             
[154] print.news_db*                                      
[155] print.nls*                                          
[156] print.noquote                                       
[157] print.numeric_version                               
[158] print.object_size*                                  
[159] print.octmode                                       
[160] print.packageDescription*                           
[161] print.packageInfo                                   
[162] print.packageIQR*                                   
[163] print.packageStatus*                                
[164] print.pairwise.htest*                               
[165] print.person*                                       
[166] print.POSIXct                                       
[167] print.POSIXlt                                       
[168] print.power.htest*                                  
[169] print.ppr*                                          
[170] print.prcomp*                                       
[171] print.princomp*                                     
[172] print.proc_time                                     
[173] print.quosure*                                      
[174] print.quosures*                                     
[175] print.R6*                                           
[176] print.R6ClassGenerator*                             
[177] print.raster*                                       
[178] print.Rd*                                           
[179] print.recordedplot*                                 
[180] print.restart                                       
[181] print.RGBcolorConverter*                            
[182] print.rlang:::list_of_conditions*                   
[183] print.rlang_box_done*                               
[184] print.rlang_box_splice*                             
[185] print.rlang_data_pronoun*                           
[186] print.rlang_dict*                                   
[187] print.rlang_dyn_array*                              
[188] print.rlang_envs*                                   
[189] print.rlang_error*                                  
[190] print.rlang_fake_data_pronoun*                      
[191] print.rlang_lambda_function*                        
[192] print.rlang_message*                                
[193] print.rlang_trace*                                  
[194] print.rlang_warning*                                
[195] print.rlang_zap*                                    
[196] print.rle                                           
[197] print.rlib_bytes*                                   
[198] print.rlib_error_3_0*                               
[199] print.rlib_trace_3_0*                               
[200] print.roman*                                        
[201] print.sass*                                         
[202] print.sass_bundle*                                  
[203] print.sass_layer*                                   
[204] print.SavedPlots*                                   
[205] print.scalar*                                       
[206] print.sessionInfo*                                  
[207] print.shiny.tag*                                    
[208] print.shiny.tag.env*                                
[209] print.shiny.tag.list*                               
[210] print.shiny.tag.query*                              
[211] print.simple.list                                   
[212] print.smooth.spline*                                
[213] print.socket*                                       
[214] print.srcfile                                       
[215] print.srcref                                        
[216] print.stepfun*                                      
[217] print.stl*                                          
[218] print.StructTS*                                     
[219] print.subdir_tests*                                 
[220] print.summarize_CRAN_check_status*                  
[221] print.summary.aov*                                  
[222] print.summary.aovlist*                              
[223] print.summary.ecdf*                                 
[224] print.summary.glm*                                  
[225] print.summary.lm*                                   
[226] print.summary.loess*                                
[227] print.summary.manova*                               
[228] print.summary.nls*                                  
[229] print.summary.packageStatus*                        
[230] print.summary.ppr*                                  
[231] print.summary.prcomp*                               
[232] print.summary.princomp*                             
[233] print.summary.table                                 
[234] print.summary.warnings                              
[235] print.summaryDefault                                
[236] print.table                                         
[237] print.tables_aov*                                   
[238] print.terms*                                        
[239] print.ts*                                           
[240] print.tskernel*                                     
[241] print.TukeyHSD*                                     
[242] print.tukeyline*                                    
[243] print.tukeysmooth*                                  
[244] print.undoc*                                        
[245] print.vctrs_bytes*                                  
[246] print.vctrs_sclr*                                   
[247] print.vctrs_unspecified*                            
[248] print.vctrs_vctr*                                   
[249] print.vignette*                                     
[250] print.warnings                                      
[251] print.xfun_raw_string*                              
[252] print.xfun_rename_seq*                              
[253] print.xfun_strict_list*                             
[254] print.xgettext*                                     
[255] print.xngettext*                                    
[256] print.xtabs*                                        
see '?methods' for accessing help and source code

Citation

For attribution, please cite this work as

Castaneda (2022, Nov. 22). R Training for GPID Team: Preliminary notions. Retrieved from https://povcalnet-team.github.io/Rtraining/posts/preliminary-notions/

BibTeX citation

@misc{castaneda2022preliminary,
  author = {Castaneda, R.Andres},
  title = {R Training for GPID Team: Preliminary notions},
  url = {https://povcalnet-team.github.io/Rtraining/posts/preliminary-notions/},
  year = {2022}
}