lapply() :

The lapply() function takes a list as input, applies a function to each element of the list, then returns a list of the same length as the original one. Since a data frame is really just a list of vectors (you can see this with as.list(flags)), we can use lapply() to apply the class() function to each column of the flags dataset.

sapply():

sapply is just lapply but it tries to return the simplest and most relevant form as output, either a vector or a matrix. If it cant, it returns a list like lapply.

head(flag_colors)

  red green blue gold white black orange
1   1     1    0    1     1     1      0
2   1     0    0    1     0     1      0
3   1     1    0    0     1     0      0
4   1     0    1    1     1     0      1
5   1     0    1    1     0     0      0
6   1     0    0    1     0     1      0

lapply(flag_colors, sum)
$red
[1] 153

$green
[1] 91

$blue
[1] 99

$gold
[1] 91

$white
[1] 146

$black
[1] 52

$orange
[1] 26

here lapply was used and each column sum was calculated

sapply(flag_colors, sum)
   red  green   blue   gold  white  black orange 
   153     91     99     91    146     52     26 

below is an example of when sapply uses matrix

lapply(flag_shapes,range)

$circles
[1] 0 4

$crosses
[1] 0 2

$saltires
[1] 0 1

$quarters
[1] 0 4

$sunstars
[1]  0 50

#here the range function generates two values 

sapply(flag_shapes, range)
circles crosses saltires quarters sunstars
[1,]       0       0        0        0        0
[2,]       4       2        1        4       50

vapply():

Whereas sapply() tries to 'guess' the correct format of the result, vapply() allows you to specify it explicitly. If the result doesn't match the format you specify, vapply() will throw an error, causing the operation to stop. This can prevent significant problems in your code that might be caused by getting unexpected return values from sapply().

sapply(flags, class) 
#gives 
   name    landmass        zone        area  population    language    religion        bars     stripes     colours         red       green        blue 
"character"   "integer"   "integer"   "integer"   "integer"   "integer"   "integer"   "integer"   "integer"   "integer"   "integer"   "integer"   "integer" 
       gold       white       black      orange     mainhue     circles     crosses    saltires    quarters    sunstars    crescent    triangle        icon 
  "integer"   "integer"   "integer"   "integer" "character"   "integer"   "integer"   "integer"   "integer"   "integer"   "integer"   "integer"   "integer" 
    animate        text     topleft    botright 
  "integer"   "integer" "character" "character" 
#which is the same as 

vapply(flags, class, character(1))

gives the same result. But in this case the character(1) means that it wants the function to return a character vector of length one, which coincides with sapply in this case.

tapply(): if we want to split our data into groups based on the value of some variable, then we apply a function to the members of each group. This is what tapply does.

interesting way to form tables for setting this up

 table(flags$landmass)

 1  2  3  4  5  6 
31 17 35 52 39 20 

#landmass takes up values 1 to 6 so if we do table(flags$landmass) it gives us
#the number of values each of them takes 

 table(flags$animate)

  0   1 
155  39 

#same with animate, its either 0 or 1 depending on yes or no 

now use tapply, that shits cool

tapply(flags$animate, flags$landmass, mean)
        1         2         3         4         5         6 
0.4193548 0.1764706 0.1142857 0.1346154 0.1538462 0.3000000 

this gives the mean of the animate, grouped according to the corresponding landmass