library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.5.2
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.5.2
## -- Attaching packages -------------------------------- tidyverse 1.2.1 --
## v tibble  1.4.2     v purrr   0.2.5
## v tidyr   0.8.2     v dplyr   0.7.8
## v readr   1.3.0     v stringr 1.3.1
## v tibble  1.4.2     v forcats 0.3.0
## Warning: package 'tidyr' was built under R version 3.5.2
## Warning: package 'purrr' was built under R version 3.5.2
## Warning: package 'dplyr' was built under R version 3.5.2
## Warning: package 'stringr' was built under R version 3.5.2
## Warning: package 'forcats' was built under R version 3.5.2
## -- Conflicts ----------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()

Lets view mpg data.frame

str(mpg)
## Classes 'tbl_df', 'tbl' and 'data.frame':    234 obs. of  11 variables:
##  $ manufacturer: chr  "audi" "audi" "audi" "audi" ...
##  $ model       : chr  "a4" "a4" "a4" "a4" ...
##  $ displ       : num  1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ...
##  $ year        : int  1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ...
##  $ cyl         : int  4 4 4 4 6 6 6 4 4 4 ...
##  $ trans       : chr  "auto(l5)" "manual(m5)" "manual(m6)" "auto(av)" ...
##  $ drv         : chr  "f" "f" "f" "f" ...
##  $ cty         : int  18 21 20 21 16 18 18 18 16 20 ...
##  $ hwy         : int  29 29 31 30 26 26 27 26 25 28 ...
##  $ fl          : chr  "p" "p" "p" "p" ...
##  $ class       : chr  "compact" "compact" "compact" "compact" ...

Tibbles

mytibble <- tibble(
  x = 1:4, 
  y = x^2, 
  z = y + 0.1
)
mytibble
## # A tibble: 4 x 3
##       x     y     z
##   <int> <dbl> <dbl>
## 1     1     1   1.1
## 2     2     4   4.1
## 3     3     9   9.1
## 4     4    16  16.1

Another way of creating Tibbles

mytribble <- tribble(
  ~x, ~y, ~z,
  1, 4.2,"a",
  3, 9.6,"b",
  4,16.8,"c"  
)
mytribble
## # A tibble: 3 x 3
##       x     y z    
##   <dbl> <dbl> <chr>
## 1     1   4.2 a    
## 2     3   9.6 b    
## 3     4  16.8 c

You can select a single column using the $ symbol:

mpg$model
##   [1] "a4"                     "a4"                    
##   [3] "a4"                     "a4"                    
##   [5] "a4"                     "a4"                    
##   [7] "a4"                     "a4 quattro"            
##   [9] "a4 quattro"             "a4 quattro"            
##  [11] "a4 quattro"             "a4 quattro"            
##  [13] "a4 quattro"             "a4 quattro"            
##  [15] "a4 quattro"             "a6 quattro"            
##  [17] "a6 quattro"             "a6 quattro"            
##  [19] "c1500 suburban 2wd"     "c1500 suburban 2wd"    
##  [21] "c1500 suburban 2wd"     "c1500 suburban 2wd"    
##  [23] "c1500 suburban 2wd"     "corvette"              
##  [25] "corvette"               "corvette"              
##  [27] "corvette"               "corvette"              
##  [29] "k1500 tahoe 4wd"        "k1500 tahoe 4wd"       
##  [31] "k1500 tahoe 4wd"        "k1500 tahoe 4wd"       
##  [33] "malibu"                 "malibu"                
##  [35] "malibu"                 "malibu"                
##  [37] "malibu"                 "caravan 2wd"           
##  [39] "caravan 2wd"            "caravan 2wd"           
##  [41] "caravan 2wd"            "caravan 2wd"           
##  [43] "caravan 2wd"            "caravan 2wd"           
##  [45] "caravan 2wd"            "caravan 2wd"           
##  [47] "caravan 2wd"            "caravan 2wd"           
##  [49] "dakota pickup 4wd"      "dakota pickup 4wd"     
##  [51] "dakota pickup 4wd"      "dakota pickup 4wd"     
##  [53] "dakota pickup 4wd"      "dakota pickup 4wd"     
##  [55] "dakota pickup 4wd"      "dakota pickup 4wd"     
##  [57] "dakota pickup 4wd"      "durango 4wd"           
##  [59] "durango 4wd"            "durango 4wd"           
##  [61] "durango 4wd"            "durango 4wd"           
##  [63] "durango 4wd"            "durango 4wd"           
##  [65] "ram 1500 pickup 4wd"    "ram 1500 pickup 4wd"   
##  [67] "ram 1500 pickup 4wd"    "ram 1500 pickup 4wd"   
##  [69] "ram 1500 pickup 4wd"    "ram 1500 pickup 4wd"   
##  [71] "ram 1500 pickup 4wd"    "ram 1500 pickup 4wd"   
##  [73] "ram 1500 pickup 4wd"    "ram 1500 pickup 4wd"   
##  [75] "expedition 2wd"         "expedition 2wd"        
##  [77] "expedition 2wd"         "explorer 4wd"          
##  [79] "explorer 4wd"           "explorer 4wd"          
##  [81] "explorer 4wd"           "explorer 4wd"          
##  [83] "explorer 4wd"           "f150 pickup 4wd"       
##  [85] "f150 pickup 4wd"        "f150 pickup 4wd"       
##  [87] "f150 pickup 4wd"        "f150 pickup 4wd"       
##  [89] "f150 pickup 4wd"        "f150 pickup 4wd"       
##  [91] "mustang"                "mustang"               
##  [93] "mustang"                "mustang"               
##  [95] "mustang"                "mustang"               
##  [97] "mustang"                "mustang"               
##  [99] "mustang"                "civic"                 
## [101] "civic"                  "civic"                 
## [103] "civic"                  "civic"                 
## [105] "civic"                  "civic"                 
## [107] "civic"                  "civic"                 
## [109] "sonata"                 "sonata"                
## [111] "sonata"                 "sonata"                
## [113] "sonata"                 "sonata"                
## [115] "sonata"                 "tiburon"               
## [117] "tiburon"                "tiburon"               
## [119] "tiburon"                "tiburon"               
## [121] "tiburon"                "tiburon"               
## [123] "grand cherokee 4wd"     "grand cherokee 4wd"    
## [125] "grand cherokee 4wd"     "grand cherokee 4wd"    
## [127] "grand cherokee 4wd"     "grand cherokee 4wd"    
## [129] "grand cherokee 4wd"     "grand cherokee 4wd"    
## [131] "range rover"            "range rover"           
## [133] "range rover"            "range rover"           
## [135] "navigator 2wd"          "navigator 2wd"         
## [137] "navigator 2wd"          "mountaineer 4wd"       
## [139] "mountaineer 4wd"        "mountaineer 4wd"       
## [141] "mountaineer 4wd"        "altima"                
## [143] "altima"                 "altima"                
## [145] "altima"                 "altima"                
## [147] "altima"                 "maxima"                
## [149] "maxima"                 "maxima"                
## [151] "pathfinder 4wd"         "pathfinder 4wd"        
## [153] "pathfinder 4wd"         "pathfinder 4wd"        
## [155] "grand prix"             "grand prix"            
## [157] "grand prix"             "grand prix"            
## [159] "grand prix"             "forester awd"          
## [161] "forester awd"           "forester awd"          
## [163] "forester awd"           "forester awd"          
## [165] "forester awd"           "impreza awd"           
## [167] "impreza awd"            "impreza awd"           
## [169] "impreza awd"            "impreza awd"           
## [171] "impreza awd"            "impreza awd"           
## [173] "impreza awd"            "4runner 4wd"           
## [175] "4runner 4wd"            "4runner 4wd"           
## [177] "4runner 4wd"            "4runner 4wd"           
## [179] "4runner 4wd"            "camry"                 
## [181] "camry"                  "camry"                 
## [183] "camry"                  "camry"                 
## [185] "camry"                  "camry"                 
## [187] "camry solara"           "camry solara"          
## [189] "camry solara"           "camry solara"          
## [191] "camry solara"           "camry solara"          
## [193] "camry solara"           "corolla"               
## [195] "corolla"                "corolla"               
## [197] "corolla"                "corolla"               
## [199] "land cruiser wagon 4wd" "land cruiser wagon 4wd"
## [201] "toyota tacoma 4wd"      "toyota tacoma 4wd"     
## [203] "toyota tacoma 4wd"      "toyota tacoma 4wd"     
## [205] "toyota tacoma 4wd"      "toyota tacoma 4wd"     
## [207] "toyota tacoma 4wd"      "gti"                   
## [209] "gti"                    "gti"                   
## [211] "gti"                    "gti"                   
## [213] "jetta"                  "jetta"                 
## [215] "jetta"                  "jetta"                 
## [217] "jetta"                  "jetta"                 
## [219] "jetta"                  "jetta"                 
## [221] "jetta"                  "new beetle"            
## [223] "new beetle"             "new beetle"            
## [225] "new beetle"             "new beetle"            
## [227] "new beetle"             "passat"                
## [229] "passat"                 "passat"                
## [231] "passat"                 "passat"                
## [233] "passat"                 "passat"

Unique Command

unique(mpg$model)
##  [1] "a4"                     "a4 quattro"            
##  [3] "a6 quattro"             "c1500 suburban 2wd"    
##  [5] "corvette"               "k1500 tahoe 4wd"       
##  [7] "malibu"                 "caravan 2wd"           
##  [9] "dakota pickup 4wd"      "durango 4wd"           
## [11] "ram 1500 pickup 4wd"    "expedition 2wd"        
## [13] "explorer 4wd"           "f150 pickup 4wd"       
## [15] "mustang"                "civic"                 
## [17] "sonata"                 "tiburon"               
## [19] "grand cherokee 4wd"     "range rover"           
## [21] "navigator 2wd"          "mountaineer 4wd"       
## [23] "altima"                 "maxima"                
## [25] "pathfinder 4wd"         "grand prix"            
## [27] "forester awd"           "impreza awd"           
## [29] "4runner 4wd"            "camry"                 
## [31] "camry solara"           "corolla"               
## [33] "land cruiser wagon 4wd" "toyota tacoma 4wd"     
## [35] "gti"                    "jetta"                 
## [37] "new beetle"             "passat"

Observe the difference between above two

If you want to select rows rather than columns from the dataframe, use square brackets [], for example:

mpg[194,]
## # A tibble: 1 x 11
##   manufacturer model  displ  year   cyl trans drv     cty   hwy fl    class
##   <chr>        <chr>  <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 toyota       corol~   1.8  1999     4 auto~ f        24    30 r     comp~

If you want a particular entry in the dataframe, provide a column number as well:

mpg[194,2]
## # A tibble: 1 x 1
##   model  
##   <chr>  
## 1 corolla

And, of course, you can also look at a range of rows and columns as well:

mpg[194:198,1:4]
## # A tibble: 5 x 4
##   manufacturer model   displ  year
##   <chr>        <chr>   <dbl> <int>
## 1 toyota       corolla   1.8  1999
## 2 toyota       corolla   1.8  1999
## 3 toyota       corolla   1.8  1999
## 4 toyota       corolla   1.8  2008
## 5 toyota       corolla   1.8  2008

Evaluate Yoursself

Q:1 How many rows and columns does tibble3 have?

tibble3 <- tibble(
  a = seq(3,9,3),
  b = a - 1,
  c = b - 1,
  d = b*c
)
tibble3
## # A tibble: 3 x 4
##       a     b     c     d
##   <dbl> <dbl> <dbl> <dbl>
## 1     3     2     1     2
## 2     6     5     4    20
## 3     9     8     7    56

Ans : 3 rows, 4 columns

Q:2 What is the fifth variable in the storms data.frame?

head(storms)
## # A tibble: 6 x 13
##   name   year month   day  hour   lat  long status category  wind pressure
##   <chr> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <chr>  <ord>    <int>    <int>
## 1 Amy    1975     6    27     0  27.5 -79   tropi~ -1          25     1013
## 2 Amy    1975     6    27     6  28.5 -79   tropi~ -1          25     1013
## 3 Amy    1975     6    27    12  29.5 -79   tropi~ -1          25     1013
## 4 Amy    1975     6    27    18  30.5 -79   tropi~ -1          25     1013
## 5 Amy    1975     6    28     0  31.5 -78.8 tropi~ -1          25     1012
## 6 Amy    1975     6    28     6  32.4 -78.7 tropi~ -1          25     1012
## # ... with 2 more variables: ts_diameter <dbl>, hu_diameter <dbl>

Ans : hour

Q:3 How many different years does the population data.frame contain?

unique(population$year)
##  [1] 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008
## [15] 2009 2010 2011 2012 2013

Ans : 19

Q:4 What is the class of the 196th car in the mpg data.frame?

mpg$class[196]
## [1] "compact"

Ans : compact