Class 7 (18th Jan)

Setting working directory

getwd()
## [1] "X:/1.Study/4th year semester 2/Biostat/Assignment 1"
setwd("X:/1.Study/4th year semester 2/Biostat/Assignment 1")

Importing Life expectancy dataset

LifeExpectancyInYears<- read.csv("life_expectancy_years.csv", header = T, check.names = F )

Subletting specific countries’ Life expectancies

as.vector(na.omit(as.numeric(unlist(LifeExpectancyInYears[LifeExpectancyInYears$country=="Sweden",]))))-> swedenlife
## Warning in
## na.omit(as.numeric(unlist(LifeExpectancyInYears[LifeExpectancyInYears$country
## == : NAs introduced by coercion
as.vector(na.omit(as.numeric(unlist(LifeExpectancyInYears[LifeExpectancyInYears$country=="India",]))))-> indialife
## Warning in
## na.omit(as.numeric(unlist(LifeExpectancyInYears[LifeExpectancyInYears$country
## == : NAs introduced by coercion
as.vector(na.omit(as.numeric(unlist(LifeExpectancyInYears[LifeExpectancyInYears$country=="Bangladesh",]))))-> banglalife
## Warning in
## na.omit(as.numeric(unlist(LifeExpectancyInYears[LifeExpectancyInYears$country
## == : NAs introduced by coercion
as.vector(na.omit(as.numeric(unlist(LifeExpectancyInYears[LifeExpectancyInYears$country=="Switzeland",]))))-> Switzerlandlife

Comparing life expactancy between countries

Box Plot
boxplot(swedenlife, indialife, banglalife, names = c("sweden", "india", "bangladesh"), ylab= "life expectancy", xlab= "Countries")

Creating data frame
data.frame(Lifeexpec= c(swedenlife, indialife, banglalife), 
           Country=c(rep("sweden", length(swedenlife)),
                     rep("India", length(indialife)),
                     rep("Bangladesh", length(banglalife))))-> countrybox
Boxplot using dataframe

~ tilde denotes the arguments inside boxplot functions are not characters but are values

boxplot(countrybox$Lifeexpec~countrybox$Country)

single sample t-test

using a mean of 10, it generates random normal distribution

single_sample = rnorm(10, mean = 10)

to tets weather mean=10, NULL HYPTHESIS= true mean is equal to 10

t.test(single_sample, mu=10) 
## 
##  One Sample t-test
## 
## data:  single_sample
## t = -0.032904, df = 9, p-value = 0.9745
## alternative hypothesis: true mean is not equal to 10
## 95 percent confidence interval:
##   9.372204 10.609795
## sample estimates:
## mean of x 
##  9.990999

Here instead of w we are seeing t, df (degrees of freedom), also here P value is 0.2169, so we can’t reject our null hypothesis

taking mean of 100

single_sample= rnorm(100, mean = 100)

now we are checking weather the mean is 10 or not, NULL HYPOTHESIS= true mean is equal to 10

t.test(single_sample, mu= 10)
## 
##  One Sample t-test
## 
## data:  single_sample
## t = 950, df = 99, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 10
## 95 percent confidence interval:
##   99.94884 100.32537
## sample estimates:
## mean of x 
##  100.1371

Here p value is way less than 0.05 so we can reject the Null hypothesis, means mean isn’t equal to 10 NOTE: while doing T test we have to assume our data is normally distributed * T test is a type of parametric test- means it needs to meet certain assumptions , one of these assumptions is * it needs to be Normally distributed

we can check weather average life expactancy of india is near ~35 years or not

t.test(indialife, mu= 35)
## 
##  One Sample t-test
## 
## data:  indialife
## t = 8.4823, df = 300, p-value = 1.022e-15
## alternative hypothesis: true mean is not equal to 35
## 95 percent confidence interval:
##  43.46090 48.57272
## sample estimates:
## mean of x 
##  46.01681

t = 8.4823, df = 300, p-value = 1.022e-15, p value is so low so our hypothesis was wrong

Now lets assume the average life expactancy is 45 years

t.test(indialife, mu= 45)
## 
##  One Sample t-test
## 
## data:  indialife
## t = 0.78289, df = 300, p-value = 0.4343
## alternative hypothesis: true mean is not equal to 45
## 95 percent confidence interval:
##  43.46090 48.57272
## sample estimates:
## mean of x 
##  46.01681

P>> 0.05 so we cant reject the assumption

  • India life expectancy isn’t normally distributes as it has increased only, but for any parametric test like t test so we can’t use this test
  • First check your data for normality then only use any type of tests

Paired T-test

Outline = F is to get rid of the outliers (values those are completely outside the range)

x= rnorm(10)
y= rnorm(10)
boxplot(x, y, names = c("X", "Y"))
boxplot(x, y, names = c("X", "Y"), outline = F)

  • By looking at plots we can say the means look same, so we can check weather their mean are same or not
  • So here NULL HYPOTHESIS = means are same
t.test(x, y)
## 
##  Welch Two Sample t-test
## 
## data:  x and y
## t = -0.87754, df = 17.842, p-value = 0.3919
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -1.1270259  0.4632136
## sample estimates:
##  mean of x  mean of y 
## -0.1028464  0.2290597

p >> 0.05 it means our assumption was right means are same

Paired

t.test(x, y, paired = T)
## 
##  Paired t-test
## 
## data:  x and y
## t = -0.76506, df = 9, p-value = 0.4638
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -1.3132943  0.6494819
## sample estimates:
## mean of the differences 
##              -0.3319062
  • The values of P is different from the above (Welch T test), as it uses a different formula
  • paired test is much more conservative than Welch t test
  • It takes care of confounding factors (means the factors that are effecting the data but aren’t visible), for example, the world wars
  • Paired Data set USes: For example comparing samples from a diseased person and a normal person (control)
  • Paired T test is similar to Welch t test but it is more designed to compare pair data set means similar datas

Creating test normal functions

x=rnorm(10)
y=rnorm(40)
Unpaired T- test

It works even x and y have different no of values

t.test(x, y)
## 
##  Welch Two Sample t-test
## 
## data:  x and y
## t = -0.12533, df = 11.52, p-value = 0.9024
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -0.8460380  0.7543989
## sample estimates:
##  mean of x  mean of y 
## 0.01088495 0.05670448

But it doesnt work as it needs x and y have same number of values (length)

t.test(x, y, paired = T) 
## Error in complete.cases(x, y): not all arguments have the same length

Q: Can we use T-test on life expectancy data? Ans: No, because Life expectancy data is not normalized so it cant be used in any parametric test

Though the following will give some results, but it should not be used as the data is not normal

t.test(swedenlife, indialife) 
## 
##  Welch Two Sample t-test
## 
## data:  swedenlife and indialife
## t = 12.011, df = 578.84, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  16.90768 23.51850
## sample estimates:
## mean of x mean of y 
##  66.22990  46.01681

Degree of freedom is more

t.test(swedenlife, indialife, var.equal = T)
## 
##  Two Sample t-test
## 
## data:  swedenlife and indialife
## t = 12.011, df = 600, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  16.90793 23.51825
## sample estimates:
## mean of x mean of y 
##  66.22990  46.01681

Degree of freedom is reduced when varience isn’t equal

t.test(swedenlife, indialife, var.equal =  F)
## 
##  Welch Two Sample t-test
## 
## data:  swedenlife and indialife
## t = 12.011, df = 578.84, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  16.90768 23.51850
## sample estimates:
## mean of x mean of y 
##  66.22990  46.01681
  • The above tests are two tailed test as the values can be either greater or lesser as we haven’t specified anything we have only told the difference is
var(swedenlife)
## [1] 344.7674

variance= 344.7674 for Sweden life expectancy

var(indialife)
## [1] 507.7486

variance= 507.7486 for India life expectancy

So to check these variance are similar or different we’ll do some tests * based on weather variance is equal or not we’ll give arguments in t.test(swedenlife, indialife, var.equal = T or F) * F test is done to check weather the variance is similar or not

NOTE: As we want to change our null hypothesis as difference of mean (First value - second value) is less, so we we’ll set our alternate hypothesis to greater so automatically our Null hypothesis will become less, as Alternate hypotheis is opposite to null hypothesis. For following we are supposing (NULL hypothesis) that mean of Sweden is less than India

t.test(swedenlife, indialife, var.equal = T, alternative = "greater")
## 
##  Two Sample t-test
## 
## data:  swedenlife and indialife
## t = 12.011, df = 600, p-value < 2.2e-16
## alternative hypothesis: true difference in means is greater than 0
## 95 percent confidence interval:
##  17.44062      Inf
## sample estimates:
## mean of x mean of y 
##  66.22990  46.01681

As the p value is very less, so hypothesis mean of Sweden is less than India was wrong , means mean of sweden must be greater than india, lets check this hypothesis too.

Now our NULL hypothesis is Mean of india India is lesser so the difference of first and second must be greater than zero, so the opposite of our null hypotheis means the alternate hypotheis must be set to less

t.test(swedenlife, indialife, var.equal = T, alternative = "less")
## 
##  Two Sample t-test
## 
## data:  swedenlife and indialife
## t = 12.011, df = 600, p-value = 1
## alternative hypothesis: true difference in means is less than 0
## 95 percent confidence interval:
##      -Inf 22.98556
## sample estimates:
## mean of x mean of y 
##  66.22990  46.01681

For this p value is greater than alpha 0.05, P= 1 here, so our hypothesis was correct

  • NOTE: difference is taken between 1st Sweden and 2nd India
  • The above tests are one tailed test as the values were defined either greater or lesser

set exact = F to ensure it doesn’t calculate exact P values, it will give approximate or threshold of P value

t.test(swedenlife, indialife, paired = T, exact= F)
## 
##  Paired t-test
## 
## data:  swedenlife and indialife
## t = 39.538, df = 300, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  19.20703 21.21915
## sample estimates:
## mean of the differences 
##                20.21309

Wilcoxon test

  • The non parametric alternative of T-test
  • Performs one and two-sample Wilcoxon tests on vectors of data
  • The latter is also known as ‘Mann-Whitney’ test
wilcox.test(swedenlife, indialife, paired = T)
## 
##  Wilcoxon signed rank test with continuity correction
## 
## data:  swedenlife and indialife
## V = 45451, p-value < 2.2e-16
## alternative hypothesis: true location shift is not equal to 0

now we know sweden is more developed so our alternative hypothesis is sweden has less life expectancy so the alternate= “greater”

wilcox.test(swedenlife, indialife, paired = T, alternative = "less") 
## 
##  Wilcoxon signed rank test with continuity correction
## 
## data:  swedenlife and indialife
## V = 45451, p-value = 1
## alternative hypothesis: true location shift is less than 0

p value is significant so the hypothesis is correct

Plotting Bangladesh and India Life expectancy

par(mfrow= c(1,2))
hist(banglalife)
hist(indialife)

Just by looking at data we cant say its normally distributed or not

boxplot(banglalife, indialife, names = c("banglalife", "indialife"))

By box plot we can assume the meanlife of bangla desh is greater then, so lets test

wilcox.test(banglalife, indialife, paired = T)
## 
##  Wilcoxon signed rank test with continuity correction
## 
## data:  banglalife and indialife
## V = 34228, p-value = 8.592e-15
## alternative hypothesis: true location shift is not equal to 0

Q: what if data from some years are missing? Ans: It becomes unpaired

wilcox.test(banglalife, indialife, paired = F)
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  banglalife and indialife
## W = 49105, p-value = 0.07421
## alternative hypothesis: true location shift is not equal to 0

The P value is less then 0.05 so it isn’t that significant * So if data isn’t paired P value isn’t very significant * So we must do a paired test

wilcox.test(banglalife, indialife, paired = T, alternative = "greater") 
## 
##  Wilcoxon signed rank test with continuity correction
## 
## data:  banglalife and indialife
## V = 34228, p-value = 4.296e-15
## alternative hypothesis: true location shift is greater than 0

Its true because, Hypothesis= Bangladesh has greater life expectancy

wilcox.test(indialife, banglalife, paired = T, alternative = "greater") 
## 
##  Wilcoxon signed rank test with continuity correction
## 
## data:  indialife and banglalife
## V = 10922, p-value = 1
## alternative hypothesis: true location shift is greater than 0

Its false as, Hypothesis= India has greater life expectancy

Order does matter in one tailed test, but it doesn’t matter in two tailed test

summary(banglalife)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   12.60   25.50   40.50   47.66   75.30   87.60
summary(indialife)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    8.16   25.40   35.20   46.02   70.80   81.80
  • T tests can only do tests with 2 variables,
  • For more than two variables we should try ANOVA or
  • Some non parametric tests like cruxcallwalis test or pair wise wilcoxon test

Installing Data packages and using them directly

install.packages("gapminder", repos = "http://cran.us.r-project.org") #installing gapminder dataset
## Installing package into 'C:/Users/govin/OneDrive/Documents/R/win-library/4.0'
## (as 'lib' is unspecified)
## package 'gapminder' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\govin\AppData\Local\Temp\Rtmpw90n0U\downloaded_packages
library(gapminder) #loading gapminder

Checking properties of Gapminder dataset

str(gapminder_unfiltered)
## tibble [3,313 x 6] (S3: tbl_df/tbl/data.frame)
##  $ country  : Factor w/ 187 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ continent: Factor w/ 6 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
##  $ year     : int [1:3313] 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##  $ lifeExp  : num [1:3313] 28.8 30.3 32 34 36.1 ...
##  $ pop      : int [1:3313] 8425333 9240934 10267083 11537966 13079460 14880372 12881816 13867957 16317921 22227415 ...
##  $ gdpPercap: num [1:3313] 779 821 853 836 740 ...
str(gapminder) #both gapminder and gapminder_unfiltered are stored in the form of tibble
## tibble [1,704 x 6] (S3: tbl_df/tbl/data.frame)
##  $ country  : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
##  $ year     : int [1:1704] 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##  $ lifeExp  : num [1:1704] 28.8 30.3 32 34 36.1 ...
##  $ pop      : int [1:1704] 8425333 9240934 10267083 11537966 13079460 14880372 12881816 13867957 16317921 22227415 ...
##  $ gdpPercap: num [1:1704] 779 821 853 836 740 ...
summary(gapminder_unfiltered) # for each of the column tells summary
##            country        continent         year         lifeExp     
##  Czech Republic:  58   Africa  : 637   Min.   :1950   Min.   :23.60  
##  Denmark       :  58   Americas: 470   1st Qu.:1967   1st Qu.:58.33  
##  Finland       :  58   Asia    : 578   Median :1982   Median :69.61  
##  Iceland       :  58   Europe  :1302   Mean   :1980   Mean   :65.24  
##  Japan         :  58   FSU     : 139   3rd Qu.:1996   3rd Qu.:73.66  
##  Netherlands   :  58   Oceania : 187   Max.   :2007   Max.   :82.67  
##  (Other)       :2965                                                 
##       pop              gdpPercap       
##  Min.   :5.941e+04   Min.   :   241.2  
##  1st Qu.:2.680e+06   1st Qu.:  2505.3  
##  Median :7.560e+06   Median :  7825.8  
##  Mean   :3.177e+07   Mean   : 11313.8  
##  3rd Qu.:1.961e+07   3rd Qu.: 17355.8  
##  Max.   :1.319e+09   Max.   :113523.1  
## 

Installing Hmisc Package

install.packages("Hmisc", repos = "http://cran.us.r-project.org") #Miscelenious functions very helpful
## Installing package into 'C:/Users/govin/OneDrive/Documents/R/win-library/4.0'
## (as 'lib' is unspecified)
## package 'Hmisc' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\govin\AppData\Local\Temp\Rtmpw90n0U\downloaded_packages
library(Hmisc)
## Loading required package: lattice
## Loading required package: survival
## Loading required package: Formula
## Loading required package: ggplot2
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
## 
##     format.pval, units
View(gapminder)
Attaching values
LifeExpectancyInYears$country # will show the vector 
##   [1] "Afghanistan"                    "Albania"                       
##   [3] "Algeria"                        "Andorra"                       
##   [5] "Angola"                         "Antigua and Barbuda"           
##   [7] "Argentina"                      "Armenia"                       
##   [9] "Australia"                      "Austria"                       
##  [11] "Azerbaijan"                     "Bahamas"                       
##  [13] "Bahrain"                        "Bangladesh"                    
##  [15] "Barbados"                       "Belarus"                       
##  [17] "Belgium"                        "Belize"                        
##  [19] "Benin"                          "Bhutan"                        
##  [21] "Bolivia"                        "Bosnia and Herzegovina"        
##  [23] "Botswana"                       "Brazil"                        
##  [25] "Brunei"                         "Bulgaria"                      
##  [27] "Burkina Faso"                   "Burundi"                       
##  [29] "Cambodia"                       "Cameroon"                      
##  [31] "Canada"                         "Cape Verde"                    
##  [33] "Central African Republic"       "Chad"                          
##  [35] "Chile"                          "China"                         
##  [37] "Colombia"                       "Comoros"                       
##  [39] "Congo, Dem. Rep."               "Congo, Rep."                   
##  [41] "Costa Rica"                     "Cote d'Ivoire"                 
##  [43] "Croatia"                        "Cuba"                          
##  [45] "Cyprus"                         "Czech Republic"                
##  [47] "Denmark"                        "Djibouti"                      
##  [49] "Dominica"                       "Dominican Republic"            
##  [51] "Ecuador"                        "Egypt"                         
##  [53] "El Salvador"                    "Equatorial Guinea"             
##  [55] "Eritrea"                        "Estonia"                       
##  [57] "Eswatini"                       "Ethiopia"                      
##  [59] "Fiji"                           "Finland"                       
##  [61] "France"                         "Gabon"                         
##  [63] "Gambia"                         "Georgia"                       
##  [65] "Germany"                        "Ghana"                         
##  [67] "Greece"                         "Grenada"                       
##  [69] "Guatemala"                      "Guinea"                        
##  [71] "Guinea-Bissau"                  "Guyana"                        
##  [73] "Haiti"                          "Honduras"                      
##  [75] "Hungary"                        "Iceland"                       
##  [77] "India"                          "Indonesia"                     
##  [79] "Iran"                           "Iraq"                          
##  [81] "Ireland"                        "Israel"                        
##  [83] "Italy"                          "Jamaica"                       
##  [85] "Japan"                          "Jordan"                        
##  [87] "Kazakhstan"                     "Kenya"                         
##  [89] "Kiribati"                       "Kuwait"                        
##  [91] "Kyrgyz Republic"                "Lao"                           
##  [93] "Latvia"                         "Lebanon"                       
##  [95] "Lesotho"                        "Liberia"                       
##  [97] "Libya"                          "Lithuania"                     
##  [99] "Luxembourg"                     "Madagascar"                    
## [101] "Malawi"                         "Malaysia"                      
## [103] "Maldives"                       "Mali"                          
## [105] "Malta"                          "Marshall Islands"              
## [107] "Mauritania"                     "Mauritius"                     
## [109] "Mexico"                         "Micronesia, Fed. Sts."         
## [111] "Moldova"                        "Mongolia"                      
## [113] "Montenegro"                     "Morocco"                       
## [115] "Mozambique"                     "Myanmar"                       
## [117] "Namibia"                        "Nepal"                         
## [119] "Netherlands"                    "New Zealand"                   
## [121] "Nicaragua"                      "Niger"                         
## [123] "Nigeria"                        "North Korea"                   
## [125] "North Macedonia"                "Norway"                        
## [127] "Oman"                           "Pakistan"                      
## [129] "Palestine"                      "Panama"                        
## [131] "Papua New Guinea"               "Paraguay"                      
## [133] "Peru"                           "Philippines"                   
## [135] "Poland"                         "Portugal"                      
## [137] "Qatar"                          "Romania"                       
## [139] "Russia"                         "Rwanda"                        
## [141] "Samoa"                          "Sao Tome and Principe"         
## [143] "Saudi Arabia"                   "Senegal"                       
## [145] "Serbia"                         "Seychelles"                    
## [147] "Sierra Leone"                   "Singapore"                     
## [149] "Slovak Republic"                "Slovenia"                      
## [151] "Solomon Islands"                "Somalia"                       
## [153] "South Africa"                   "South Korea"                   
## [155] "South Sudan"                    "Spain"                         
## [157] "Sri Lanka"                      "St. Lucia"                     
## [159] "St. Vincent and the Grenadines" "Sudan"                         
## [161] "Suriname"                       "Sweden"                        
## [163] "Switzerland"                    "Syria"                         
## [165] "Tajikistan"                     "Tanzania"                      
## [167] "Thailand"                       "Timor-Leste"                   
## [169] "Togo"                           "Tonga"                         
## [171] "Trinidad and Tobago"            "Tunisia"                       
## [173] "Turkey"                         "Turkmenistan"                  
## [175] "Uganda"                         "Ukraine"                       
## [177] "United Arab Emirates"           "United Kingdom"                
## [179] "United States"                  "Uruguay"                       
## [181] "Uzbekistan"                     "Vanuatu"                       
## [183] "Venezuela"                      "Vietnam"                       
## [185] "Yemen"                          "Zambia"                        
## [187] "Zimbabwe"
country # just typing country will not show the vector
## Error in eval(expr, envir, enclos): object 'country' not found
attach(LifeExpectancyInYears)

now just by typing country well get country values

To detach

detach(LifeExpectancyInYears)

detach just reverses the attach function

Exploratory data analysis (EDA)

describe(gapminder_unfiltered) #part of Hmisc package, it has taken the dataset and summarised it
## gapminder_unfiltered 
## 
##  6  Variables      3313  Observations
## --------------------------------------------------------------------------------
## country 
##        n  missing distinct 
##     3313        0      187 
## 
## lowest : Afghanistan        Albania            Algeria            Angola             Argentina         
## highest: Vietnam            West Bank and Gaza Yemen, Rep.        Zambia             Zimbabwe          
## --------------------------------------------------------------------------------
## continent 
##        n  missing distinct 
##     3313        0        6 
## 
## lowest : Africa   Americas Asia     Europe   FSU     
## highest: Americas Asia     Europe   FSU      Oceania 
##                                                                 
## Value        Africa Americas     Asia   Europe      FSU  Oceania
## Frequency       637      470      578     1302      139      187
## Proportion    0.192    0.142    0.174    0.393    0.042    0.056
## --------------------------------------------------------------------------------
## year 
##        n  missing distinct     Info     Mean      Gmd      .05      .10 
##     3313        0       58    0.998     1980    19.52     1952     1957 
##      .25      .50      .75      .90      .95 
##     1967     1982     1996     2002     2007 
## 
## lowest : 1950 1951 1952 1953 1954, highest: 2003 2004 2005 2006 2007
## --------------------------------------------------------------------------------
## lifeExp 
##        n  missing distinct     Info     Mean      Gmd      .05      .10 
##     3313        0     2571        1    65.24    12.73    41.22    45.37 
##      .25      .50      .75      .90      .95 
##    58.33    69.61    73.66    77.12    78.68 
## 
## lowest : 23.599 28.801 30.000 30.015 30.331, highest: 82.208 82.270 82.360 82.603 82.670
## --------------------------------------------------------------------------------
## pop 
##         n   missing  distinct      Info      Mean       Gmd       .05       .10 
##      3313         0      3312         1  31773251  50168977    235605    436150 
##       .25       .50       .75       .90       .95 
##   2680018   7559776  19610538  56737055 121365965 
## 
## lowest :      59412      59461      60011      60427      61325
## highest: 1110396331 1164970000 1230075000 1280400000 1318683096
## --------------------------------------------------------------------------------
## gdpPercap 
##        n  missing distinct     Info     Mean      Gmd      .05      .10 
##     3313        0     3313        1    11314    11542    665.7    887.9 
##      .25      .50      .75      .90      .95 
##   2505.3   7825.8  17355.7  26592.7  31534.9 
## 
## lowest :    241.1659    277.5519    298.8462    299.8503    312.1884
## highest:  82010.9780  95458.1118 108382.3529 109347.8670 113523.1329
## --------------------------------------------------------------------------------
dim(gapminder_unfiltered) #dimentions of the object, row and column of the tibble
## [1] 3313    6
nrow(gapminder_unfiltered) # shows number of rows 
## [1] 3313
ncol(gapminder_unfiltered) # shows number of columns
## [1] 6
colnames(gapminder_unfiltered) # shows column names
## [1] "country"   "continent" "year"      "lifeExp"   "pop"       "gdpPercap"
rownames(gapminder_unfiltered) # shows row names
##    [1] "1"    "2"    "3"    "4"    "5"    "6"    "7"    "8"    "9"    "10"  
##   [11] "11"   "12"   "13"   "14"   "15"   "16"   "17"   "18"   "19"   "20"  
##   [21] "21"   "22"   "23"   "24"   "25"   "26"   "27"   "28"   "29"   "30"  
##   [31] "31"   "32"   "33"   "34"   "35"   "36"   "37"   "38"   "39"   "40"  
##   [41] "41"   "42"   "43"   "44"   "45"   "46"   "47"   "48"   "49"   "50"  
##   [51] "51"   "52"   "53"   "54"   "55"   "56"   "57"   "58"   "59"   "60"  
##   [61] "61"   "62"   "63"   "64"   "65"   "66"   "67"   "68"   "69"   "70"  
##   [71] "71"   "72"   "73"   "74"   "75"   "76"   "77"   "78"   "79"   "80"  
##   [81] "81"   "82"   "83"   "84"   "85"   "86"   "87"   "88"   "89"   "90"  
##   [91] "91"   "92"   "93"   "94"   "95"   "96"   "97"   "98"   "99"   "100" 
##  [101] "101"  "102"  "103"  "104"  "105"  "106"  "107"  "108"  "109"  "110" 
##  [111] "111"  "112"  "113"  "114"  "115"  "116"  "117"  "118"  "119"  "120" 
##  [121] "121"  "122"  "123"  "124"  "125"  "126"  "127"  "128"  "129"  "130" 
##  [131] "131"  "132"  "133"  "134"  "135"  "136"  "137"  "138"  "139"  "140" 
##  [141] "141"  "142"  "143"  "144"  "145"  "146"  "147"  "148"  "149"  "150" 
##  [151] "151"  "152"  "153"  "154"  "155"  "156"  "157"  "158"  "159"  "160" 
##  [161] "161"  "162"  "163"  "164"  "165"  "166"  "167"  "168"  "169"  "170" 
##  [171] "171"  "172"  "173"  "174"  "175"  "176"  "177"  "178"  "179"  "180" 
##  [181] "181"  "182"  "183"  "184"  "185"  "186"  "187"  "188"  "189"  "190" 
##  [191] "191"  "192"  "193"  "194"  "195"  "196"  "197"  "198"  "199"  "200" 
##  [201] "201"  "202"  "203"  "204"  "205"  "206"  "207"  "208"  "209"  "210" 
##  [211] "211"  "212"  "213"  "214"  "215"  "216"  "217"  "218"  "219"  "220" 
##  [221] "221"  "222"  "223"  "224"  "225"  "226"  "227"  "228"  "229"  "230" 
##  [231] "231"  "232"  "233"  "234"  "235"  "236"  "237"  "238"  "239"  "240" 
##  [241] "241"  "242"  "243"  "244"  "245"  "246"  "247"  "248"  "249"  "250" 
##  [251] "251"  "252"  "253"  "254"  "255"  "256"  "257"  "258"  "259"  "260" 
##  [261] "261"  "262"  "263"  "264"  "265"  "266"  "267"  "268"  "269"  "270" 
##  [271] "271"  "272"  "273"  "274"  "275"  "276"  "277"  "278"  "279"  "280" 
##  [281] "281"  "282"  "283"  "284"  "285"  "286"  "287"  "288"  "289"  "290" 
##  [291] "291"  "292"  "293"  "294"  "295"  "296"  "297"  "298"  "299"  "300" 
##  [301] "301"  "302"  "303"  "304"  "305"  "306"  "307"  "308"  "309"  "310" 
##  [311] "311"  "312"  "313"  "314"  "315"  "316"  "317"  "318"  "319"  "320" 
##  [321] "321"  "322"  "323"  "324"  "325"  "326"  "327"  "328"  "329"  "330" 
##  [331] "331"  "332"  "333"  "334"  "335"  "336"  "337"  "338"  "339"  "340" 
##  [341] "341"  "342"  "343"  "344"  "345"  "346"  "347"  "348"  "349"  "350" 
##  [351] "351"  "352"  "353"  "354"  "355"  "356"  "357"  "358"  "359"  "360" 
##  [361] "361"  "362"  "363"  "364"  "365"  "366"  "367"  "368"  "369"  "370" 
##  [371] "371"  "372"  "373"  "374"  "375"  "376"  "377"  "378"  "379"  "380" 
##  [381] "381"  "382"  "383"  "384"  "385"  "386"  "387"  "388"  "389"  "390" 
##  [391] "391"  "392"  "393"  "394"  "395"  "396"  "397"  "398"  "399"  "400" 
##  [401] "401"  "402"  "403"  "404"  "405"  "406"  "407"  "408"  "409"  "410" 
##  [411] "411"  "412"  "413"  "414"  "415"  "416"  "417"  "418"  "419"  "420" 
##  [421] "421"  "422"  "423"  "424"  "425"  "426"  "427"  "428"  "429"  "430" 
##  [431] "431"  "432"  "433"  "434"  "435"  "436"  "437"  "438"  "439"  "440" 
##  [441] "441"  "442"  "443"  "444"  "445"  "446"  "447"  "448"  "449"  "450" 
##  [451] "451"  "452"  "453"  "454"  "455"  "456"  "457"  "458"  "459"  "460" 
##  [461] "461"  "462"  "463"  "464"  "465"  "466"  "467"  "468"  "469"  "470" 
##  [471] "471"  "472"  "473"  "474"  "475"  "476"  "477"  "478"  "479"  "480" 
##  [481] "481"  "482"  "483"  "484"  "485"  "486"  "487"  "488"  "489"  "490" 
##  [491] "491"  "492"  "493"  "494"  "495"  "496"  "497"  "498"  "499"  "500" 
##  [501] "501"  "502"  "503"  "504"  "505"  "506"  "507"  "508"  "509"  "510" 
##  [511] "511"  "512"  "513"  "514"  "515"  "516"  "517"  "518"  "519"  "520" 
##  [521] "521"  "522"  "523"  "524"  "525"  "526"  "527"  "528"  "529"  "530" 
##  [531] "531"  "532"  "533"  "534"  "535"  "536"  "537"  "538"  "539"  "540" 
##  [541] "541"  "542"  "543"  "544"  "545"  "546"  "547"  "548"  "549"  "550" 
##  [551] "551"  "552"  "553"  "554"  "555"  "556"  "557"  "558"  "559"  "560" 
##  [561] "561"  "562"  "563"  "564"  "565"  "566"  "567"  "568"  "569"  "570" 
##  [571] "571"  "572"  "573"  "574"  "575"  "576"  "577"  "578"  "579"  "580" 
##  [581] "581"  "582"  "583"  "584"  "585"  "586"  "587"  "588"  "589"  "590" 
##  [591] "591"  "592"  "593"  "594"  "595"  "596"  "597"  "598"  "599"  "600" 
##  [601] "601"  "602"  "603"  "604"  "605"  "606"  "607"  "608"  "609"  "610" 
##  [611] "611"  "612"  "613"  "614"  "615"  "616"  "617"  "618"  "619"  "620" 
##  [621] "621"  "622"  "623"  "624"  "625"  "626"  "627"  "628"  "629"  "630" 
##  [631] "631"  "632"  "633"  "634"  "635"  "636"  "637"  "638"  "639"  "640" 
##  [641] "641"  "642"  "643"  "644"  "645"  "646"  "647"  "648"  "649"  "650" 
##  [651] "651"  "652"  "653"  "654"  "655"  "656"  "657"  "658"  "659"  "660" 
##  [661] "661"  "662"  "663"  "664"  "665"  "666"  "667"  "668"  "669"  "670" 
##  [671] "671"  "672"  "673"  "674"  "675"  "676"  "677"  "678"  "679"  "680" 
##  [681] "681"  "682"  "683"  "684"  "685"  "686"  "687"  "688"  "689"  "690" 
##  [691] "691"  "692"  "693"  "694"  "695"  "696"  "697"  "698"  "699"  "700" 
##  [701] "701"  "702"  "703"  "704"  "705"  "706"  "707"  "708"  "709"  "710" 
##  [711] "711"  "712"  "713"  "714"  "715"  "716"  "717"  "718"  "719"  "720" 
##  [721] "721"  "722"  "723"  "724"  "725"  "726"  "727"  "728"  "729"  "730" 
##  [731] "731"  "732"  "733"  "734"  "735"  "736"  "737"  "738"  "739"  "740" 
##  [741] "741"  "742"  "743"  "744"  "745"  "746"  "747"  "748"  "749"  "750" 
##  [751] "751"  "752"  "753"  "754"  "755"  "756"  "757"  "758"  "759"  "760" 
##  [761] "761"  "762"  "763"  "764"  "765"  "766"  "767"  "768"  "769"  "770" 
##  [771] "771"  "772"  "773"  "774"  "775"  "776"  "777"  "778"  "779"  "780" 
##  [781] "781"  "782"  "783"  "784"  "785"  "786"  "787"  "788"  "789"  "790" 
##  [791] "791"  "792"  "793"  "794"  "795"  "796"  "797"  "798"  "799"  "800" 
##  [801] "801"  "802"  "803"  "804"  "805"  "806"  "807"  "808"  "809"  "810" 
##  [811] "811"  "812"  "813"  "814"  "815"  "816"  "817"  "818"  "819"  "820" 
##  [821] "821"  "822"  "823"  "824"  "825"  "826"  "827"  "828"  "829"  "830" 
##  [831] "831"  "832"  "833"  "834"  "835"  "836"  "837"  "838"  "839"  "840" 
##  [841] "841"  "842"  "843"  "844"  "845"  "846"  "847"  "848"  "849"  "850" 
##  [851] "851"  "852"  "853"  "854"  "855"  "856"  "857"  "858"  "859"  "860" 
##  [861] "861"  "862"  "863"  "864"  "865"  "866"  "867"  "868"  "869"  "870" 
##  [871] "871"  "872"  "873"  "874"  "875"  "876"  "877"  "878"  "879"  "880" 
##  [881] "881"  "882"  "883"  "884"  "885"  "886"  "887"  "888"  "889"  "890" 
##  [891] "891"  "892"  "893"  "894"  "895"  "896"  "897"  "898"  "899"  "900" 
##  [901] "901"  "902"  "903"  "904"  "905"  "906"  "907"  "908"  "909"  "910" 
##  [911] "911"  "912"  "913"  "914"  "915"  "916"  "917"  "918"  "919"  "920" 
##  [921] "921"  "922"  "923"  "924"  "925"  "926"  "927"  "928"  "929"  "930" 
##  [931] "931"  "932"  "933"  "934"  "935"  "936"  "937"  "938"  "939"  "940" 
##  [941] "941"  "942"  "943"  "944"  "945"  "946"  "947"  "948"  "949"  "950" 
##  [951] "951"  "952"  "953"  "954"  "955"  "956"  "957"  "958"  "959"  "960" 
##  [961] "961"  "962"  "963"  "964"  "965"  "966"  "967"  "968"  "969"  "970" 
##  [971] "971"  "972"  "973"  "974"  "975"  "976"  "977"  "978"  "979"  "980" 
##  [981] "981"  "982"  "983"  "984"  "985"  "986"  "987"  "988"  "989"  "990" 
##  [991] "991"  "992"  "993"  "994"  "995"  "996"  "997"  "998"  "999"  "1000"
## [1001] "1001" "1002" "1003" "1004" "1005" "1006" "1007" "1008" "1009" "1010"
## [1011] "1011" "1012" "1013" "1014" "1015" "1016" "1017" "1018" "1019" "1020"
## [1021] "1021" "1022" "1023" "1024" "1025" "1026" "1027" "1028" "1029" "1030"
## [1031] "1031" "1032" "1033" "1034" "1035" "1036" "1037" "1038" "1039" "1040"
## [1041] "1041" "1042" "1043" "1044" "1045" "1046" "1047" "1048" "1049" "1050"
## [1051] "1051" "1052" "1053" "1054" "1055" "1056" "1057" "1058" "1059" "1060"
## [1061] "1061" "1062" "1063" "1064" "1065" "1066" "1067" "1068" "1069" "1070"
## [1071] "1071" "1072" "1073" "1074" "1075" "1076" "1077" "1078" "1079" "1080"
## [1081] "1081" "1082" "1083" "1084" "1085" "1086" "1087" "1088" "1089" "1090"
## [1091] "1091" "1092" "1093" "1094" "1095" "1096" "1097" "1098" "1099" "1100"
## [1101] "1101" "1102" "1103" "1104" "1105" "1106" "1107" "1108" "1109" "1110"
## [1111] "1111" "1112" "1113" "1114" "1115" "1116" "1117" "1118" "1119" "1120"
## [1121] "1121" "1122" "1123" "1124" "1125" "1126" "1127" "1128" "1129" "1130"
## [1131] "1131" "1132" "1133" "1134" "1135" "1136" "1137" "1138" "1139" "1140"
## [1141] "1141" "1142" "1143" "1144" "1145" "1146" "1147" "1148" "1149" "1150"
## [1151] "1151" "1152" "1153" "1154" "1155" "1156" "1157" "1158" "1159" "1160"
## [1161] "1161" "1162" "1163" "1164" "1165" "1166" "1167" "1168" "1169" "1170"
## [1171] "1171" "1172" "1173" "1174" "1175" "1176" "1177" "1178" "1179" "1180"
## [1181] "1181" "1182" "1183" "1184" "1185" "1186" "1187" "1188" "1189" "1190"
## [1191] "1191" "1192" "1193" "1194" "1195" "1196" "1197" "1198" "1199" "1200"
## [1201] "1201" "1202" "1203" "1204" "1205" "1206" "1207" "1208" "1209" "1210"
## [1211] "1211" "1212" "1213" "1214" "1215" "1216" "1217" "1218" "1219" "1220"
## [1221] "1221" "1222" "1223" "1224" "1225" "1226" "1227" "1228" "1229" "1230"
## [1231] "1231" "1232" "1233" "1234" "1235" "1236" "1237" "1238" "1239" "1240"
## [1241] "1241" "1242" "1243" "1244" "1245" "1246" "1247" "1248" "1249" "1250"
## [1251] "1251" "1252" "1253" "1254" "1255" "1256" "1257" "1258" "1259" "1260"
## [1261] "1261" "1262" "1263" "1264" "1265" "1266" "1267" "1268" "1269" "1270"
## [1271] "1271" "1272" "1273" "1274" "1275" "1276" "1277" "1278" "1279" "1280"
## [1281] "1281" "1282" "1283" "1284" "1285" "1286" "1287" "1288" "1289" "1290"
## [1291] "1291" "1292" "1293" "1294" "1295" "1296" "1297" "1298" "1299" "1300"
## [1301] "1301" "1302" "1303" "1304" "1305" "1306" "1307" "1308" "1309" "1310"
## [1311] "1311" "1312" "1313" "1314" "1315" "1316" "1317" "1318" "1319" "1320"
## [1321] "1321" "1322" "1323" "1324" "1325" "1326" "1327" "1328" "1329" "1330"
## [1331] "1331" "1332" "1333" "1334" "1335" "1336" "1337" "1338" "1339" "1340"
## [1341] "1341" "1342" "1343" "1344" "1345" "1346" "1347" "1348" "1349" "1350"
## [1351] "1351" "1352" "1353" "1354" "1355" "1356" "1357" "1358" "1359" "1360"
## [1361] "1361" "1362" "1363" "1364" "1365" "1366" "1367" "1368" "1369" "1370"
## [1371] "1371" "1372" "1373" "1374" "1375" "1376" "1377" "1378" "1379" "1380"
## [1381] "1381" "1382" "1383" "1384" "1385" "1386" "1387" "1388" "1389" "1390"
## [1391] "1391" "1392" "1393" "1394" "1395" "1396" "1397" "1398" "1399" "1400"
## [1401] "1401" "1402" "1403" "1404" "1405" "1406" "1407" "1408" "1409" "1410"
## [1411] "1411" "1412" "1413" "1414" "1415" "1416" "1417" "1418" "1419" "1420"
## [1421] "1421" "1422" "1423" "1424" "1425" "1426" "1427" "1428" "1429" "1430"
## [1431] "1431" "1432" "1433" "1434" "1435" "1436" "1437" "1438" "1439" "1440"
## [1441] "1441" "1442" "1443" "1444" "1445" "1446" "1447" "1448" "1449" "1450"
## [1451] "1451" "1452" "1453" "1454" "1455" "1456" "1457" "1458" "1459" "1460"
## [1461] "1461" "1462" "1463" "1464" "1465" "1466" "1467" "1468" "1469" "1470"
## [1471] "1471" "1472" "1473" "1474" "1475" "1476" "1477" "1478" "1479" "1480"
## [1481] "1481" "1482" "1483" "1484" "1485" "1486" "1487" "1488" "1489" "1490"
## [1491] "1491" "1492" "1493" "1494" "1495" "1496" "1497" "1498" "1499" "1500"
## [1501] "1501" "1502" "1503" "1504" "1505" "1506" "1507" "1508" "1509" "1510"
## [1511] "1511" "1512" "1513" "1514" "1515" "1516" "1517" "1518" "1519" "1520"
## [1521] "1521" "1522" "1523" "1524" "1525" "1526" "1527" "1528" "1529" "1530"
## [1531] "1531" "1532" "1533" "1534" "1535" "1536" "1537" "1538" "1539" "1540"
## [1541] "1541" "1542" "1543" "1544" "1545" "1546" "1547" "1548" "1549" "1550"
## [1551] "1551" "1552" "1553" "1554" "1555" "1556" "1557" "1558" "1559" "1560"
## [1561] "1561" "1562" "1563" "1564" "1565" "1566" "1567" "1568" "1569" "1570"
## [1571] "1571" "1572" "1573" "1574" "1575" "1576" "1577" "1578" "1579" "1580"
## [1581] "1581" "1582" "1583" "1584" "1585" "1586" "1587" "1588" "1589" "1590"
## [1591] "1591" "1592" "1593" "1594" "1595" "1596" "1597" "1598" "1599" "1600"
## [1601] "1601" "1602" "1603" "1604" "1605" "1606" "1607" "1608" "1609" "1610"
## [1611] "1611" "1612" "1613" "1614" "1615" "1616" "1617" "1618" "1619" "1620"
## [1621] "1621" "1622" "1623" "1624" "1625" "1626" "1627" "1628" "1629" "1630"
## [1631] "1631" "1632" "1633" "1634" "1635" "1636" "1637" "1638" "1639" "1640"
## [1641] "1641" "1642" "1643" "1644" "1645" "1646" "1647" "1648" "1649" "1650"
## [1651] "1651" "1652" "1653" "1654" "1655" "1656" "1657" "1658" "1659" "1660"
## [1661] "1661" "1662" "1663" "1664" "1665" "1666" "1667" "1668" "1669" "1670"
## [1671] "1671" "1672" "1673" "1674" "1675" "1676" "1677" "1678" "1679" "1680"
## [1681] "1681" "1682" "1683" "1684" "1685" "1686" "1687" "1688" "1689" "1690"
## [1691] "1691" "1692" "1693" "1694" "1695" "1696" "1697" "1698" "1699" "1700"
## [1701] "1701" "1702" "1703" "1704" "1705" "1706" "1707" "1708" "1709" "1710"
## [1711] "1711" "1712" "1713" "1714" "1715" "1716" "1717" "1718" "1719" "1720"
## [1721] "1721" "1722" "1723" "1724" "1725" "1726" "1727" "1728" "1729" "1730"
## [1731] "1731" "1732" "1733" "1734" "1735" "1736" "1737" "1738" "1739" "1740"
## [1741] "1741" "1742" "1743" "1744" "1745" "1746" "1747" "1748" "1749" "1750"
## [1751] "1751" "1752" "1753" "1754" "1755" "1756" "1757" "1758" "1759" "1760"
## [1761] "1761" "1762" "1763" "1764" "1765" "1766" "1767" "1768" "1769" "1770"
## [1771] "1771" "1772" "1773" "1774" "1775" "1776" "1777" "1778" "1779" "1780"
## [1781] "1781" "1782" "1783" "1784" "1785" "1786" "1787" "1788" "1789" "1790"
## [1791] "1791" "1792" "1793" "1794" "1795" "1796" "1797" "1798" "1799" "1800"
## [1801] "1801" "1802" "1803" "1804" "1805" "1806" "1807" "1808" "1809" "1810"
## [1811] "1811" "1812" "1813" "1814" "1815" "1816" "1817" "1818" "1819" "1820"
## [1821] "1821" "1822" "1823" "1824" "1825" "1826" "1827" "1828" "1829" "1830"
## [1831] "1831" "1832" "1833" "1834" "1835" "1836" "1837" "1838" "1839" "1840"
## [1841] "1841" "1842" "1843" "1844" "1845" "1846" "1847" "1848" "1849" "1850"
## [1851] "1851" "1852" "1853" "1854" "1855" "1856" "1857" "1858" "1859" "1860"
## [1861] "1861" "1862" "1863" "1864" "1865" "1866" "1867" "1868" "1869" "1870"
## [1871] "1871" "1872" "1873" "1874" "1875" "1876" "1877" "1878" "1879" "1880"
## [1881] "1881" "1882" "1883" "1884" "1885" "1886" "1887" "1888" "1889" "1890"
## [1891] "1891" "1892" "1893" "1894" "1895" "1896" "1897" "1898" "1899" "1900"
## [1901] "1901" "1902" "1903" "1904" "1905" "1906" "1907" "1908" "1909" "1910"
## [1911] "1911" "1912" "1913" "1914" "1915" "1916" "1917" "1918" "1919" "1920"
## [1921] "1921" "1922" "1923" "1924" "1925" "1926" "1927" "1928" "1929" "1930"
## [1931] "1931" "1932" "1933" "1934" "1935" "1936" "1937" "1938" "1939" "1940"
## [1941] "1941" "1942" "1943" "1944" "1945" "1946" "1947" "1948" "1949" "1950"
## [1951] "1951" "1952" "1953" "1954" "1955" "1956" "1957" "1958" "1959" "1960"
## [1961] "1961" "1962" "1963" "1964" "1965" "1966" "1967" "1968" "1969" "1970"
## [1971] "1971" "1972" "1973" "1974" "1975" "1976" "1977" "1978" "1979" "1980"
## [1981] "1981" "1982" "1983" "1984" "1985" "1986" "1987" "1988" "1989" "1990"
## [1991] "1991" "1992" "1993" "1994" "1995" "1996" "1997" "1998" "1999" "2000"
## [2001] "2001" "2002" "2003" "2004" "2005" "2006" "2007" "2008" "2009" "2010"
## [2011] "2011" "2012" "2013" "2014" "2015" "2016" "2017" "2018" "2019" "2020"
## [2021] "2021" "2022" "2023" "2024" "2025" "2026" "2027" "2028" "2029" "2030"
## [2031] "2031" "2032" "2033" "2034" "2035" "2036" "2037" "2038" "2039" "2040"
## [2041] "2041" "2042" "2043" "2044" "2045" "2046" "2047" "2048" "2049" "2050"
## [2051] "2051" "2052" "2053" "2054" "2055" "2056" "2057" "2058" "2059" "2060"
## [2061] "2061" "2062" "2063" "2064" "2065" "2066" "2067" "2068" "2069" "2070"
## [2071] "2071" "2072" "2073" "2074" "2075" "2076" "2077" "2078" "2079" "2080"
## [2081] "2081" "2082" "2083" "2084" "2085" "2086" "2087" "2088" "2089" "2090"
## [2091] "2091" "2092" "2093" "2094" "2095" "2096" "2097" "2098" "2099" "2100"
## [2101] "2101" "2102" "2103" "2104" "2105" "2106" "2107" "2108" "2109" "2110"
## [2111] "2111" "2112" "2113" "2114" "2115" "2116" "2117" "2118" "2119" "2120"
## [2121] "2121" "2122" "2123" "2124" "2125" "2126" "2127" "2128" "2129" "2130"
## [2131] "2131" "2132" "2133" "2134" "2135" "2136" "2137" "2138" "2139" "2140"
## [2141] "2141" "2142" "2143" "2144" "2145" "2146" "2147" "2148" "2149" "2150"
## [2151] "2151" "2152" "2153" "2154" "2155" "2156" "2157" "2158" "2159" "2160"
## [2161] "2161" "2162" "2163" "2164" "2165" "2166" "2167" "2168" "2169" "2170"
## [2171] "2171" "2172" "2173" "2174" "2175" "2176" "2177" "2178" "2179" "2180"
## [2181] "2181" "2182" "2183" "2184" "2185" "2186" "2187" "2188" "2189" "2190"
## [2191] "2191" "2192" "2193" "2194" "2195" "2196" "2197" "2198" "2199" "2200"
## [2201] "2201" "2202" "2203" "2204" "2205" "2206" "2207" "2208" "2209" "2210"
## [2211] "2211" "2212" "2213" "2214" "2215" "2216" "2217" "2218" "2219" "2220"
## [2221] "2221" "2222" "2223" "2224" "2225" "2226" "2227" "2228" "2229" "2230"
## [2231] "2231" "2232" "2233" "2234" "2235" "2236" "2237" "2238" "2239" "2240"
## [2241] "2241" "2242" "2243" "2244" "2245" "2246" "2247" "2248" "2249" "2250"
## [2251] "2251" "2252" "2253" "2254" "2255" "2256" "2257" "2258" "2259" "2260"
## [2261] "2261" "2262" "2263" "2264" "2265" "2266" "2267" "2268" "2269" "2270"
## [2271] "2271" "2272" "2273" "2274" "2275" "2276" "2277" "2278" "2279" "2280"
## [2281] "2281" "2282" "2283" "2284" "2285" "2286" "2287" "2288" "2289" "2290"
## [2291] "2291" "2292" "2293" "2294" "2295" "2296" "2297" "2298" "2299" "2300"
## [2301] "2301" "2302" "2303" "2304" "2305" "2306" "2307" "2308" "2309" "2310"
## [2311] "2311" "2312" "2313" "2314" "2315" "2316" "2317" "2318" "2319" "2320"
## [2321] "2321" "2322" "2323" "2324" "2325" "2326" "2327" "2328" "2329" "2330"
## [2331] "2331" "2332" "2333" "2334" "2335" "2336" "2337" "2338" "2339" "2340"
## [2341] "2341" "2342" "2343" "2344" "2345" "2346" "2347" "2348" "2349" "2350"
## [2351] "2351" "2352" "2353" "2354" "2355" "2356" "2357" "2358" "2359" "2360"
## [2361] "2361" "2362" "2363" "2364" "2365" "2366" "2367" "2368" "2369" "2370"
## [2371] "2371" "2372" "2373" "2374" "2375" "2376" "2377" "2378" "2379" "2380"
## [2381] "2381" "2382" "2383" "2384" "2385" "2386" "2387" "2388" "2389" "2390"
## [2391] "2391" "2392" "2393" "2394" "2395" "2396" "2397" "2398" "2399" "2400"
## [2401] "2401" "2402" "2403" "2404" "2405" "2406" "2407" "2408" "2409" "2410"
## [2411] "2411" "2412" "2413" "2414" "2415" "2416" "2417" "2418" "2419" "2420"
## [2421] "2421" "2422" "2423" "2424" "2425" "2426" "2427" "2428" "2429" "2430"
## [2431] "2431" "2432" "2433" "2434" "2435" "2436" "2437" "2438" "2439" "2440"
## [2441] "2441" "2442" "2443" "2444" "2445" "2446" "2447" "2448" "2449" "2450"
## [2451] "2451" "2452" "2453" "2454" "2455" "2456" "2457" "2458" "2459" "2460"
## [2461] "2461" "2462" "2463" "2464" "2465" "2466" "2467" "2468" "2469" "2470"
## [2471] "2471" "2472" "2473" "2474" "2475" "2476" "2477" "2478" "2479" "2480"
## [2481] "2481" "2482" "2483" "2484" "2485" "2486" "2487" "2488" "2489" "2490"
## [2491] "2491" "2492" "2493" "2494" "2495" "2496" "2497" "2498" "2499" "2500"
## [2501] "2501" "2502" "2503" "2504" "2505" "2506" "2507" "2508" "2509" "2510"
## [2511] "2511" "2512" "2513" "2514" "2515" "2516" "2517" "2518" "2519" "2520"
## [2521] "2521" "2522" "2523" "2524" "2525" "2526" "2527" "2528" "2529" "2530"
## [2531] "2531" "2532" "2533" "2534" "2535" "2536" "2537" "2538" "2539" "2540"
## [2541] "2541" "2542" "2543" "2544" "2545" "2546" "2547" "2548" "2549" "2550"
## [2551] "2551" "2552" "2553" "2554" "2555" "2556" "2557" "2558" "2559" "2560"
## [2561] "2561" "2562" "2563" "2564" "2565" "2566" "2567" "2568" "2569" "2570"
## [2571] "2571" "2572" "2573" "2574" "2575" "2576" "2577" "2578" "2579" "2580"
## [2581] "2581" "2582" "2583" "2584" "2585" "2586" "2587" "2588" "2589" "2590"
## [2591] "2591" "2592" "2593" "2594" "2595" "2596" "2597" "2598" "2599" "2600"
## [2601] "2601" "2602" "2603" "2604" "2605" "2606" "2607" "2608" "2609" "2610"
## [2611] "2611" "2612" "2613" "2614" "2615" "2616" "2617" "2618" "2619" "2620"
## [2621] "2621" "2622" "2623" "2624" "2625" "2626" "2627" "2628" "2629" "2630"
## [2631] "2631" "2632" "2633" "2634" "2635" "2636" "2637" "2638" "2639" "2640"
## [2641] "2641" "2642" "2643" "2644" "2645" "2646" "2647" "2648" "2649" "2650"
## [2651] "2651" "2652" "2653" "2654" "2655" "2656" "2657" "2658" "2659" "2660"
## [2661] "2661" "2662" "2663" "2664" "2665" "2666" "2667" "2668" "2669" "2670"
## [2671] "2671" "2672" "2673" "2674" "2675" "2676" "2677" "2678" "2679" "2680"
## [2681] "2681" "2682" "2683" "2684" "2685" "2686" "2687" "2688" "2689" "2690"
## [2691] "2691" "2692" "2693" "2694" "2695" "2696" "2697" "2698" "2699" "2700"
## [2701] "2701" "2702" "2703" "2704" "2705" "2706" "2707" "2708" "2709" "2710"
## [2711] "2711" "2712" "2713" "2714" "2715" "2716" "2717" "2718" "2719" "2720"
## [2721] "2721" "2722" "2723" "2724" "2725" "2726" "2727" "2728" "2729" "2730"
## [2731] "2731" "2732" "2733" "2734" "2735" "2736" "2737" "2738" "2739" "2740"
## [2741] "2741" "2742" "2743" "2744" "2745" "2746" "2747" "2748" "2749" "2750"
## [2751] "2751" "2752" "2753" "2754" "2755" "2756" "2757" "2758" "2759" "2760"
## [2761] "2761" "2762" "2763" "2764" "2765" "2766" "2767" "2768" "2769" "2770"
## [2771] "2771" "2772" "2773" "2774" "2775" "2776" "2777" "2778" "2779" "2780"
## [2781] "2781" "2782" "2783" "2784" "2785" "2786" "2787" "2788" "2789" "2790"
## [2791] "2791" "2792" "2793" "2794" "2795" "2796" "2797" "2798" "2799" "2800"
## [2801] "2801" "2802" "2803" "2804" "2805" "2806" "2807" "2808" "2809" "2810"
## [2811] "2811" "2812" "2813" "2814" "2815" "2816" "2817" "2818" "2819" "2820"
## [2821] "2821" "2822" "2823" "2824" "2825" "2826" "2827" "2828" "2829" "2830"
## [2831] "2831" "2832" "2833" "2834" "2835" "2836" "2837" "2838" "2839" "2840"
## [2841] "2841" "2842" "2843" "2844" "2845" "2846" "2847" "2848" "2849" "2850"
## [2851] "2851" "2852" "2853" "2854" "2855" "2856" "2857" "2858" "2859" "2860"
## [2861] "2861" "2862" "2863" "2864" "2865" "2866" "2867" "2868" "2869" "2870"
## [2871] "2871" "2872" "2873" "2874" "2875" "2876" "2877" "2878" "2879" "2880"
## [2881] "2881" "2882" "2883" "2884" "2885" "2886" "2887" "2888" "2889" "2890"
## [2891] "2891" "2892" "2893" "2894" "2895" "2896" "2897" "2898" "2899" "2900"
## [2901] "2901" "2902" "2903" "2904" "2905" "2906" "2907" "2908" "2909" "2910"
## [2911] "2911" "2912" "2913" "2914" "2915" "2916" "2917" "2918" "2919" "2920"
## [2921] "2921" "2922" "2923" "2924" "2925" "2926" "2927" "2928" "2929" "2930"
## [2931] "2931" "2932" "2933" "2934" "2935" "2936" "2937" "2938" "2939" "2940"
## [2941] "2941" "2942" "2943" "2944" "2945" "2946" "2947" "2948" "2949" "2950"
## [2951] "2951" "2952" "2953" "2954" "2955" "2956" "2957" "2958" "2959" "2960"
## [2961] "2961" "2962" "2963" "2964" "2965" "2966" "2967" "2968" "2969" "2970"
## [2971] "2971" "2972" "2973" "2974" "2975" "2976" "2977" "2978" "2979" "2980"
## [2981] "2981" "2982" "2983" "2984" "2985" "2986" "2987" "2988" "2989" "2990"
## [2991] "2991" "2992" "2993" "2994" "2995" "2996" "2997" "2998" "2999" "3000"
## [3001] "3001" "3002" "3003" "3004" "3005" "3006" "3007" "3008" "3009" "3010"
## [3011] "3011" "3012" "3013" "3014" "3015" "3016" "3017" "3018" "3019" "3020"
## [3021] "3021" "3022" "3023" "3024" "3025" "3026" "3027" "3028" "3029" "3030"
## [3031] "3031" "3032" "3033" "3034" "3035" "3036" "3037" "3038" "3039" "3040"
## [3041] "3041" "3042" "3043" "3044" "3045" "3046" "3047" "3048" "3049" "3050"
## [3051] "3051" "3052" "3053" "3054" "3055" "3056" "3057" "3058" "3059" "3060"
## [3061] "3061" "3062" "3063" "3064" "3065" "3066" "3067" "3068" "3069" "3070"
## [3071] "3071" "3072" "3073" "3074" "3075" "3076" "3077" "3078" "3079" "3080"
## [3081] "3081" "3082" "3083" "3084" "3085" "3086" "3087" "3088" "3089" "3090"
## [3091] "3091" "3092" "3093" "3094" "3095" "3096" "3097" "3098" "3099" "3100"
## [3101] "3101" "3102" "3103" "3104" "3105" "3106" "3107" "3108" "3109" "3110"
## [3111] "3111" "3112" "3113" "3114" "3115" "3116" "3117" "3118" "3119" "3120"
## [3121] "3121" "3122" "3123" "3124" "3125" "3126" "3127" "3128" "3129" "3130"
## [3131] "3131" "3132" "3133" "3134" "3135" "3136" "3137" "3138" "3139" "3140"
## [3141] "3141" "3142" "3143" "3144" "3145" "3146" "3147" "3148" "3149" "3150"
## [3151] "3151" "3152" "3153" "3154" "3155" "3156" "3157" "3158" "3159" "3160"
## [3161] "3161" "3162" "3163" "3164" "3165" "3166" "3167" "3168" "3169" "3170"
## [3171] "3171" "3172" "3173" "3174" "3175" "3176" "3177" "3178" "3179" "3180"
## [3181] "3181" "3182" "3183" "3184" "3185" "3186" "3187" "3188" "3189" "3190"
## [3191] "3191" "3192" "3193" "3194" "3195" "3196" "3197" "3198" "3199" "3200"
## [3201] "3201" "3202" "3203" "3204" "3205" "3206" "3207" "3208" "3209" "3210"
## [3211] "3211" "3212" "3213" "3214" "3215" "3216" "3217" "3218" "3219" "3220"
## [3221] "3221" "3222" "3223" "3224" "3225" "3226" "3227" "3228" "3229" "3230"
## [3231] "3231" "3232" "3233" "3234" "3235" "3236" "3237" "3238" "3239" "3240"
## [3241] "3241" "3242" "3243" "3244" "3245" "3246" "3247" "3248" "3249" "3250"
## [3251] "3251" "3252" "3253" "3254" "3255" "3256" "3257" "3258" "3259" "3260"
## [3261] "3261" "3262" "3263" "3264" "3265" "3266" "3267" "3268" "3269" "3270"
## [3271] "3271" "3272" "3273" "3274" "3275" "3276" "3277" "3278" "3279" "3280"
## [3281] "3281" "3282" "3283" "3284" "3285" "3286" "3287" "3288" "3289" "3290"
## [3291] "3291" "3292" "3293" "3294" "3295" "3296" "3297" "3298" "3299" "3300"
## [3301] "3301" "3302" "3303" "3304" "3305" "3306" "3307" "3308" "3309" "3310"
## [3311] "3311" "3312" "3313"
summary(gapminder_unfiltered) # summarised the data sets
##            country        continent         year         lifeExp     
##  Czech Republic:  58   Africa  : 637   Min.   :1950   Min.   :23.60  
##  Denmark       :  58   Americas: 470   1st Qu.:1967   1st Qu.:58.33  
##  Finland       :  58   Asia    : 578   Median :1982   Median :69.61  
##  Iceland       :  58   Europe  :1302   Mean   :1980   Mean   :65.24  
##  Japan         :  58   FSU     : 139   3rd Qu.:1996   3rd Qu.:73.66  
##  Netherlands   :  58   Oceania : 187   Max.   :2007   Max.   :82.67  
##  (Other)       :2965                                                 
##       pop              gdpPercap       
##  Min.   :5.941e+04   Min.   :   241.2  
##  1st Qu.:2.680e+06   1st Qu.:  2505.3  
##  Median :7.560e+06   Median :  7825.8  
##  Mean   :3.177e+07   Mean   : 11313.8  
##  3rd Qu.:1.961e+07   3rd Qu.: 17355.8  
##  Max.   :1.319e+09   Max.   :113523.1  
## 

Q: How is summary different from describe?? Ans: Summary is more consice and it comes with base package, in general summary is more than enough

Subsetting the data

gm.subset <- gapminder[gapminder$year %in% c(2002, 2007),]

Perform a function for each country

Calulate mean GDP per capita per country
aggregate(gm.subset$gdpPercap, by= list(gm.subset$country), FUN = mean)
##                      Group.1          x
## 1                Afghanistan   850.6572
## 2                    Albania  5270.6206
## 3                    Algeria  5755.7039
## 4                     Angola  3785.2593
## 5                  Argentina 10788.5102
## 6                  Australia 32561.5611
## 7                    Austria 34272.0502
## 8                    Bahrain 26599.8038
## 9                 Bangladesh  1263.8221
## 10                   Belgium 32089.2444
## 11                     Benin  1407.0814
## 12                   Bolivia  3617.6999
## 13    Bosnia and Herzegovina  6732.6370
## 14                  Botswana 11786.7284
## 15                    Brazil  8598.5068
## 16                  Bulgaria  9188.7853
## 17              Burkina Faso  1127.3391
## 18                   Burundi   438.2371
## 19                  Cambodia  1305.0024
## 20                  Cameroon  1988.0533
## 21                    Canada 34824.1000
## 22  Central African Republic   722.3536
## 23                      Chad  1430.1228
## 24                     Chile 11975.2113
## 25                     China  4039.1979
## 26                  Colombia  6380.9202
## 27                   Comoros  1030.9797
## 28          Congo, Dem. Rep.   259.3589
## 29               Congo, Rep.  3558.3099
## 30                Costa Rica  8684.2543
## 31             Cote d'Ivoire  1596.7755
## 32                   Croatia 13123.8058
## 33                      Cuba  7644.3748
## 34            Czech Republic 20214.7594
## 35                   Denmark 33722.4594
## 36                  Djibouti  1995.3712
## 37        Dominican Republic  5294.5915
## 38                   Ecuador  6323.1534
## 39                     Egypt  5167.8927
## 40               El Salvador  5539.9611
## 41         Equatorial Guinea  9928.7928
## 42                   Eritrea   703.3598
## 43                  Ethiopia   610.4296
## 44                   Finland 30705.8375
## 45                    France 29698.0245
## 46                     Gabon 12864.0992
## 47                    Gambia   706.6677
## 48                   Germany 31103.0882
## 49                     Ghana  1219.7967
## 50                    Greece 25026.3333
## 51                 Guatemala  5022.1987
## 52                    Guinea   944.1189
## 53             Guinea-Bissau   577.4682
## 54                     Haiti  1236.0010
## 55                  Honduras  3324.0298
## 56          Hong Kong, China 34966.9969
## 57                   Hungary 16426.4400
## 58                   Iceland 33671.9956
## 59                     India  2099.4899
## 60                 Indonesia  3207.2822
## 61                      Iran 10423.2382
## 62                      Iraq  4430.8896
## 63                   Ireland 37376.5229
## 64                    Israel 23714.4361
## 65                     Italy 28268.9089
## 66                   Jamaica  7157.8276
## 67                     Japan 30130.3300
## 68                    Jordan  4182.1892
## 69                     Kenya  1375.3820
## 70          Korea, Dem. Rep.  1619.9118
## 71               Korea, Rep. 21291.0640
## 72                    Kuwait 41208.5477
## 73                   Lebanon  9887.4988
## 74                   Lesotho  1422.2580
## 75                   Liberia   472.9949
## 76                     Libya 10796.0884
## 77                Madagascar   969.7036
## 78                    Malawi   712.3865
## 79                  Malaysia 11329.3169
## 80                      Mali   996.9957
## 81                Mauritania  1691.0855
## 82                 Mauritius  9989.4035
## 83                    Mexico 11360.0077
## 84                  Mongolia  2618.2558
## 85                Montenegro  7905.5452
## 86                   Morocco  3539.3354
## 87                Mozambique   728.6518
## 88                   Myanmar   777.5000
## 89                   Namibia  4441.6926
## 90                     Nepal  1074.2830
## 91               Netherlands 35261.3455
## 92               New Zealand 24187.4052
## 93                 Nicaragua  2611.9349
## 94                     Niger   610.3757
## 95                   Nigeria  1814.6318
## 96                    Norway 47020.5827
## 97                      Oman 21045.5149
## 98                  Pakistan  2349.3300
## 99                    Panama  8582.6088
## 100                 Paraguay  3978.2564
## 101                     Peru  6658.9628
## 102              Philippines  2920.7010
## 103                   Poland 13696.0819
## 104                 Portugal 20240.2778
## 105              Puerto Rico 19092.1576
## 106                  Reunion  6993.1439
## 107                  Romania  9346.9178
## 108                   Rwanda   824.3711
## 109    Sao Tome and Principe  1475.7637
## 110             Saudi Arabia 20334.6866
## 111                  Senegal  1616.0537
## 112                   Serbia  8511.3050
## 113             Sierra Leone   781.0152
## 114                Singapore 41583.1425
## 115          Slovak Republic 16158.5464
## 116                 Slovenia 23214.1385
## 117                  Somalia   904.1114
## 118             South Africa  8490.3021
## 119                    Spain 26828.2677
## 120                Sri Lanka  3492.7371
## 121                    Sudan  2297.8967
## 122                Swaziland  4320.7988
## 123                   Sweden 31600.6896
## 124              Switzerland 35993.6884
## 125                    Syria  4137.7367
## 126                   Taiwan 25976.8501
## 127                 Tanzania  1003.2782
## 128                 Thailand  6685.7919
## 129                     Togo   884.5953
## 130      Trinidad and Tobago 14734.5547
## 131                  Tunisia  6407.9093
## 132                   Turkey  7483.1811
## 133                   Uganda   992.0506
## 134           United Kingdom 31341.1302
## 135            United States 41024.3763
## 136                  Uruguay  9169.2325
## 137                Venezuela 10010.4268
## 138                  Vietnam  2103.0165
## 139       West Bank and Gaza  3770.4187
## 140              Yemen, Rep.  2257.7954
## 141                   Zambia  1171.4128
## 142                 Zimbabwe   570.8740

Now this is giving us mean gdp per capita for each country

There are three arguments in this function * 1st is the Vector (gm.subset\(gdpPercap) on which we want to apply the function * 2nd the criteria ( list(gm.subset\)country)) on which we want to apply * 3rd is the Funtion (FUN = mean)

Similarly For mean GDP per capita for per continent

aggregate(gm.subset$gdpPercap, by= list(gm.subset$continent), FUN = mean) 
##    Group.1         x
## 1   Africa  2844.209
## 2 Americas 10145.354
## 3     Asia 11323.559
## 4   Europe 23383.107
## 5  Oceania 28374.483

Now this is giving us mean gdp per capita for each continent

Similarly For mean GDP per capita for both continent and country

aggregate(gm.subset$gdpPercap, by= list(gm.subset$country, gm.subset$continent), FUN = mean)
##                      Group.1  Group.2          x
## 1                    Algeria   Africa  5755.7039
## 2                     Angola   Africa  3785.2593
## 3                      Benin   Africa  1407.0814
## 4                   Botswana   Africa 11786.7284
## 5               Burkina Faso   Africa  1127.3391
## 6                    Burundi   Africa   438.2371
## 7                   Cameroon   Africa  1988.0533
## 8   Central African Republic   Africa   722.3536
## 9                       Chad   Africa  1430.1228
## 10                   Comoros   Africa  1030.9797
## 11          Congo, Dem. Rep.   Africa   259.3589
## 12               Congo, Rep.   Africa  3558.3099
## 13             Cote d'Ivoire   Africa  1596.7755
## 14                  Djibouti   Africa  1995.3712
## 15                     Egypt   Africa  5167.8927
## 16         Equatorial Guinea   Africa  9928.7928
## 17                   Eritrea   Africa   703.3598
## 18                  Ethiopia   Africa   610.4296
## 19                     Gabon   Africa 12864.0992
## 20                    Gambia   Africa   706.6677
## 21                     Ghana   Africa  1219.7967
## 22                    Guinea   Africa   944.1189
## 23             Guinea-Bissau   Africa   577.4682
## 24                     Kenya   Africa  1375.3820
## 25                   Lesotho   Africa  1422.2580
## 26                   Liberia   Africa   472.9949
## 27                     Libya   Africa 10796.0884
## 28                Madagascar   Africa   969.7036
## 29                    Malawi   Africa   712.3865
## 30                      Mali   Africa   996.9957
## 31                Mauritania   Africa  1691.0855
## 32                 Mauritius   Africa  9989.4035
## 33                   Morocco   Africa  3539.3354
## 34                Mozambique   Africa   728.6518
## 35                   Namibia   Africa  4441.6926
## 36                     Niger   Africa   610.3757
## 37                   Nigeria   Africa  1814.6318
## 38                   Reunion   Africa  6993.1439
## 39                    Rwanda   Africa   824.3711
## 40     Sao Tome and Principe   Africa  1475.7637
## 41                   Senegal   Africa  1616.0537
## 42              Sierra Leone   Africa   781.0152
## 43                   Somalia   Africa   904.1114
## 44              South Africa   Africa  8490.3021
## 45                     Sudan   Africa  2297.8967
## 46                 Swaziland   Africa  4320.7988
## 47                  Tanzania   Africa  1003.2782
## 48                      Togo   Africa   884.5953
## 49                   Tunisia   Africa  6407.9093
## 50                    Uganda   Africa   992.0506
## 51                    Zambia   Africa  1171.4128
## 52                  Zimbabwe   Africa   570.8740
## 53                 Argentina Americas 10788.5102
## 54                   Bolivia Americas  3617.6999
## 55                    Brazil Americas  8598.5068
## 56                    Canada Americas 34824.1000
## 57                     Chile Americas 11975.2113
## 58                  Colombia Americas  6380.9202
## 59                Costa Rica Americas  8684.2543
## 60                      Cuba Americas  7644.3748
## 61        Dominican Republic Americas  5294.5915
## 62                   Ecuador Americas  6323.1534
## 63               El Salvador Americas  5539.9611
## 64                 Guatemala Americas  5022.1987
## 65                     Haiti Americas  1236.0010
## 66                  Honduras Americas  3324.0298
## 67                   Jamaica Americas  7157.8276
## 68                    Mexico Americas 11360.0077
## 69                 Nicaragua Americas  2611.9349
## 70                    Panama Americas  8582.6088
## 71                  Paraguay Americas  3978.2564
## 72                      Peru Americas  6658.9628
## 73               Puerto Rico Americas 19092.1576
## 74       Trinidad and Tobago Americas 14734.5547
## 75             United States Americas 41024.3763
## 76                   Uruguay Americas  9169.2325
## 77                 Venezuela Americas 10010.4268
## 78               Afghanistan     Asia   850.6572
## 79                   Bahrain     Asia 26599.8038
## 80                Bangladesh     Asia  1263.8221
## 81                  Cambodia     Asia  1305.0024
## 82                     China     Asia  4039.1979
## 83          Hong Kong, China     Asia 34966.9969
## 84                     India     Asia  2099.4899
## 85                 Indonesia     Asia  3207.2822
## 86                      Iran     Asia 10423.2382
## 87                      Iraq     Asia  4430.8896
## 88                    Israel     Asia 23714.4361
## 89                     Japan     Asia 30130.3300
## 90                    Jordan     Asia  4182.1892
## 91          Korea, Dem. Rep.     Asia  1619.9118
## 92               Korea, Rep.     Asia 21291.0640
## 93                    Kuwait     Asia 41208.5477
## 94                   Lebanon     Asia  9887.4988
## 95                  Malaysia     Asia 11329.3169
## 96                  Mongolia     Asia  2618.2558
## 97                   Myanmar     Asia   777.5000
## 98                     Nepal     Asia  1074.2830
## 99                      Oman     Asia 21045.5149
## 100                 Pakistan     Asia  2349.3300
## 101              Philippines     Asia  2920.7010
## 102             Saudi Arabia     Asia 20334.6866
## 103                Singapore     Asia 41583.1425
## 104                Sri Lanka     Asia  3492.7371
## 105                    Syria     Asia  4137.7367
## 106                   Taiwan     Asia 25976.8501
## 107                 Thailand     Asia  6685.7919
## 108                  Vietnam     Asia  2103.0165
## 109       West Bank and Gaza     Asia  3770.4187
## 110              Yemen, Rep.     Asia  2257.7954
## 111                  Albania   Europe  5270.6206
## 112                  Austria   Europe 34272.0502
## 113                  Belgium   Europe 32089.2444
## 114   Bosnia and Herzegovina   Europe  6732.6370
## 115                 Bulgaria   Europe  9188.7853
## 116                  Croatia   Europe 13123.8058
## 117           Czech Republic   Europe 20214.7594
## 118                  Denmark   Europe 33722.4594
## 119                  Finland   Europe 30705.8375
## 120                   France   Europe 29698.0245
## 121                  Germany   Europe 31103.0882
## 122                   Greece   Europe 25026.3333
## 123                  Hungary   Europe 16426.4400
## 124                  Iceland   Europe 33671.9956
## 125                  Ireland   Europe 37376.5229
## 126                    Italy   Europe 28268.9089
## 127               Montenegro   Europe  7905.5452
## 128              Netherlands   Europe 35261.3455
## 129                   Norway   Europe 47020.5827
## 130                   Poland   Europe 13696.0819
## 131                 Portugal   Europe 20240.2778
## 132                  Romania   Europe  9346.9178
## 133                   Serbia   Europe  8511.3050
## 134          Slovak Republic   Europe 16158.5464
## 135                 Slovenia   Europe 23214.1385
## 136                    Spain   Europe 26828.2677
## 137                   Sweden   Europe 31600.6896
## 138              Switzerland   Europe 35993.6884
## 139                   Turkey   Europe  7483.1811
## 140           United Kingdom   Europe 31341.1302
## 141                Australia  Oceania 32561.5611
## 142              New Zealand  Oceania 24187.4052

Using sapply(), tapply(), and apply()

sapply(), checks data type of every column

sapply(gapminder, class)
##   country continent      year   lifeExp       pop gdpPercap 
##  "factor"  "factor" "integer" "numeric" "integer" "numeric"

Gapminder is a datatype and class is a function it can be anything which you want to know

To find mean for each column
sapply(gapminder, mean)
## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA
##      country    continent         year      lifeExp          pop    gdpPercap 
##           NA           NA 1.979500e+03 5.947444e+01 2.960121e+07 7.215327e+03

Mean can’t be calculated as there are character entries in some columns

As we saw above some columns are character some are numeric and if we want to perform some function on some specific column, so we can use apply function

apply(gapminder, 2, class)
##     country   continent        year     lifeExp         pop   gdpPercap 
## "character" "character" "character" "character" "character" "character"

NOTE: Here 2= column

Calculating mean only for numeric columns
apply(gapminder[,5:6], 2, mean)
##          pop    gdpPercap 
## 29601212.325     7215.327
perform function on each row
apply(gapminder[,5:6], 1, mean)
##    [1]   4213056.22   4620877.43   5133968.05   5769401.10   6540099.99
##    [6]   7440579.06   6441397.01   6934404.70   8159285.17  11114025.17
##   [11]  12634565.87  15945448.79    642149.03    739223.64    865224.94
##   [16]    993410.10   1133433.71   1256290.50   1391863.94   1539529.97
##   [21]   1664497.72   1715615.53   1756558.11   1803230.01   4640987.00
##   [26]   5136934.99   5501749.41   6381873.00   7382484.83   8578857.21
##   [31]  10019749.08  11630318.68  13151698.11  14538406.15  15646215.02
##   [36]  16669719.68   2117807.81   2282594.47   2415142.14   2626495.89
##   [41]   2950165.64   3082841.82   3509570.48   3938330.10   4369307.92
##   [46]   4938650.57   5434439.64   6212636.62   8941433.66   9808697.43
##   [51]  10645458.08  11471138.98  12394621.02  13496953.51  14675185.95
##   [56]  15815028.84  16984127.71  18107215.14  19169959.32  20157353.19
##   [61]   4350625.80   4861759.32   5403592.61   5943395.06   6596894.31
##   [66]   7046217.10   7601838.50   8139568.94   8752700.88   9296120.47
##   [71]   9788739.88  10234305.68   3466954.54   3487351.30   3570307.36
##   [76]   3694916.30   3780431.31   3794089.71   3798105.04   3801295.41
##   [81]   3971005.51   4049485.96   4090364.80   4117954.75     65157.04
##   [86]     75145.40     92308.14    108493.34    124534.33    158375.05
##   [91]    198589.07    236568.01    274263.29    309426.51    339900.28
##   [96]    369184.52  23443771.62  25683064.82  28419987.67  31411302.59
##  [101]  35379962.62  40214482.94  46537541.49  51882496.49  56852708.41
##  [106]  61658130.39  67828963.20  75224865.13   4369374.05   4499412.98
##  [111]   4614695.60   4784824.52   4862886.07   4920458.99   4938641.42
##  [116]   4946362.78   5035598.79   5113674.10   5171227.94   5212959.30
##  [121]    869688.88    963066.30   1076422.25   1214184.92   1381246.40
##  [126]   1584648.08   1821440.45   2122506.93   2491431.10   3033656.49
##  [131]   3513742.94   4039877.64   1442996.16   1606932.84   1798049.49
##  [136]   2021625.94   2284426.17   2541632.05   2822690.26   3079561.35
##  [141]   3448206.35   3848257.07   4224273.63   4561487.07   1395986.77
##  [146]   1538676.99   1675354.84   1793586.18   1910930.08   2044764.24
##  [151]   2088409.81   2171645.56   2129279.89   1805883.18   2085717.49
##  [156]   2279822.15    221579.62    237778.62    256873.83    277377.85
##  [161]    310807.31    392343.43    487449.07    578694.94    675284.06
##  [166]    772591.57    820675.30    825850.43  28302334.47  32776829.18
##  [171]  38021363.29  44026626.43  50422521.86  57160305.56  64484984.92
##  [176]  71472941.55  77991462.14  84277338.49  89961171.61  95009856.40
##  [181]   3638672.14   3827131.34   4008600.17   4157901.50   4291398.75
##  [186]   4402317.12   4450161.10   4490098.93   4332404.31   4036013.69
##  [191]   3834747.89   3666769.40   2235261.13   2357016.59   2460177.26
##  [196]   2564364.91   2717370.37   2945158.69   3317701.60   3793731.53
##  [201]   4439617.38   5176894.65   6126123.32   7163710.02   1222978.65
##  [206]   1333948.78   1481135.10   1665700.99   1765223.55   1917485.55
##  [211]   2290484.80   2563322.41   2904933.85   3061036.56   3510762.20
##  [216]   4195467.54   2347102.23   2661485.02   3042057.96   3480295.22
##  [221]   3725513.81   3489565.99   3636554.74   4186237.45   5075388.15
##  [226]   5891848.14   6463801.61   7066785.89   2505119.83   2680618.02
##  [231]   2897516.30   3168507.23   3511356.07   3980824.22   4626599.49
##  [236]   5391634.83   6234482.08   7098751.67   7965961.01   8849167.55
##  [241]   7398475.58   8511321.98   9499655.74  10417921.79  11151735.29
##  [246]  11909245.44  12612399.40  13288163.26  14274922.44  15167398.96
##  [251]  15967798.48  16713230.12    646383.16    696737.42    762335.53
##  [256]    867387.03    964165.01   1084321.19   1238963.88   1420426.94
##  [261]   1632935.95   1848626.75   2024375.85   2184872.01   1341820.33
##  [266]   1448081.75   1575903.41   1748581.91   1950086.05   2194696.99
##  [271]   2437957.95   2749953.69   3215237.53   3781507.98   4418447.59
##  [276]   5120255.53   3190779.49   3526370.81   3982888.55   4432007.33
##  [281]   4861509.01   5302274.88   5746103.83   6234450.53   6790295.06
##  [286]   7305023.53   7753912.39   8148956.32 278131963.72 318704287.99
##  [291] 332885243.84 377275306.35 431015338.45 471727870.62 500140981.21
##  [296] 542018189.45 582485827.89 615038644.62 640201559.64 659344027.56
##  [301]   6176457.56   7244158.40   8506188.68   9883352.86  11273077.33
##  [306]  12549113.90  13884520.79  15484574.11  17104082.82  18831973.68
##  [311]  20506991.13  22117278.29     77519.50     86069.57     96547.82
##  [316]    109627.01    125982.29    152955.80    174955.05    198214.99
##  [321]    227837.95    264577.81    307728.91    355973.07   7050392.77
##  [326]   7789418.93   8743665.16   9970967.30  11504286.95  13240832.88
##  [331]  15323584.37  17741158.89  20836300.36  23899649.09  27690046.58
##  [336]  32303518.28    428505.31    471386.53    525194.39    591218.97
##  [341]    671835.58    770014.09    889807.25   1034148.10   1206544.62
##  [346]   1402215.58   1666139.53   1902121.28    464472.00    557645.01
##  [351]    674323.97    796439.36    919957.07   1057191.94   1214814.87
##  [356]   1402720.46   1589688.21   1762392.02   1921328.72   2071764.53
##  [361]   1489203.80   1650750.45   1917068.43   2373461.03   3037037.10
##  [366]   3731045.87   4514276.86   5381627.48   6387122.04   7313876.63
##  [371]   8127187.40   9007476.88   1942674.12   1997790.12   2041017.45
##  [376]   2090663.15   2117237.05   2164989.19   2213294.91   2249066.29
##  [381]   2251230.40   2227235.30   2246324.19   2253965.61   3006691.77
##  [386]   3323422.09   3629776.88   4072511.13   4418326.72   4772184.25
##  [391]   4898270.46   5123685.96   5364426.42   5494219.50   5616669.82
##  [396]   5712967.55   4566029.57   4761007.17   4815209.43   4923254.22
##  [401]   4937633.23   5088357.58   5159540.61   5163953.72   5164999.51
##  [406]   5158377.76   5136945.61   5125788.65   2171846.19   2249465.33
##  [411]   2330241.16   2427368.61   2505231.10   2554420.95   2569749.02
##  [416]   2576070.09   2598899.87   2656733.67   2703429.75   2751699.21
##  [421]     32909.26     37357.98     46459.49     65318.53     91271.11
##  [426]    115887.88    154435.23    156952.55    193266.58    209901.51
##  [431]    224662.13    249228.24   1246371.86   1462365.20   1727548.07
##  [436]   2025399.86   2336759.44   2652740.99   2985605.05   3329098.42
##  [441]   3677112.61   3997985.55   4327442.90   4662823.69   1776137.56
##  [446]   2031082.77   2342896.56   2718501.54   3151966.00   3642772.81
##  [451]   4186531.90   4775819.89   5377748.85   5959624.23   6463503.52
##  [456]   6881276.63  11112363.91  12505599.96  14087501.17  15841501.44
##  [461]  17404720.50  19393324.25  22842657.36  26401473.73  29702996.38
##  [466]  33069232.09  36658656.80  40135062.09   1022956.65   1179613.26
##  [471]   1375731.90   1618642.80   1897711.62   2143862.46   2239485.67
##  [476]   2423167.22   2639546.62   2894296.91   3179516.28   3472708.18
##  [481]    108669.82    116674.05    124901.42    130389.80    139137.71
##  [486]     96816.78    143205.41    171105.45    194485.03    221392.74
##  [491]    251665.25    281677.54    719544.47    771477.58    833499.50
##  [496]    910393.90   1130350.66   1256573.88   1318910.94   1458240.07
##  [501]   1834511.43   2029616.24   2207815.18   2453613.18  10430651.57
##  [506]  11407996.45  12572895.73  13930406.56  15385469.12  17309177.90
##  [511]  19056166.93  21500051.87  26044490.18  29930908.44  33973663.53
##  [516]  38256288.90   2048462.26   2165772.71   2250407.42   2308332.82
##  [521]   2327007.94   2377253.71   2422733.08   2476435.01   2530843.08
##  [526]   2579064.98   2610621.80   2635833.54  21233348.40  22159762.92
##  [531]  23567280.24  24790999.96  25874053.60  26591655.82  27226929.45
##  [536]  27826083.22  28699441.40  29324658.89  29976980.52  30557193.01
##  [541]    212497.74    219940.10    231146.23    248681.38    274689.47
##  [546]    364056.29    384493.68    446130.70    499630.58    570455.92
##  [551]    655912.86    734036.74    142402.62    161835.46    187309.83
##  [556]    220163.89    258928.54    304579.38    358179.40    424508.83
##  [561]    513024.81    618210.37    729213.29    844555.87  34576548.06
##  [566]  35514628.41  36876009.73  38191599.31  39367552.09  39090642.96
##  [571]  39178648.77  38871468.59  40312134.65  41019430.94  41190353.40
##  [576]  41216583.19   2790956.15   3196165.78   3678219.02   4245669.35
##  [581]   4677649.11   5269543.11   5700607.02   7084474.00   8139831.53
##  [586]   9209646.62  10275931.49  11437332.80   3868390.35   4050567.15
##  [591]   4227125.10   4362477.05   4450676.41   4661337.26   4900874.21
##  [596]   4995305.26   5171485.25   5260559.85   5313188.63   5366914.21
##  [601]   1574404.62   1821746.58   2105804.18   2347007.77   2576806.20
##  [606]   2854155.00   3200225.25   3665326.24   4245694.23   4904279.66
##  [611]   5591754.17   6289057.03   1332379.60   1438651.13   1570344.69
##  [616]   1726063.38   1906064.33   2113950.34   2355677.13   2825533.79
##  [621]   3495684.17   4024851.72   4404381.79   4974378.33    290476.43
##  [626]    300763.40    314171.02    301001.29    313090.61    372996.36
##  [631]    413412.56    464130.21    525841.77    597252.33    666517.35
##  [636]    736310.12   1601664.18   1754713.94   1940963.29   2159794.53
##  [641]   2349977.73   2455214.15   2600205.08   2879013.01   3164069.15
##  [646]   3457443.36   3804460.68   4252007.82    759823.96    886305.24
##  [651]   1046226.58   1251613.63   1483837.92   1529219.10   1836284.88
##  [656]   2187613.05   2540214.35   2935558.73   3340213.86   3743655.67
##  [661]   1064477.21   1369964.54   1654946.32   1864498.98   2062007.96
##  [666]   2297443.07   2639530.27   2802274.24   2927226.80   3262147.82
##  [671]   3396342.51   3510068.49   4754631.84   4922520.09   5035275.18
##  [676]   5116374.32   5202129.83   5324422.92   5359040.50   5312863.24
##  [681]   5179609.81   5128198.39   5049078.47   4987058.47     77614.84
##  [686]     87177.00     96201.58    105997.95    112536.53    120738.98
##  [691]    128633.30    135799.60    142078.20    149626.55    159596.60
##  [696]    169055.89 186000273.28 204500295.03 227000329.17 253000350.39
##  [701] 283500362.02 317000406.67 354000427.86 394000488.26 436000582.20
##  [706] 479500729.41 517087146.88 555199391.61  41026374.84  45062429.45
##  [711]  49514424.64  54671881.22  60641555.55  68363191.35  76672258.44
##  [716]  84638874.18  92409191.57  99640559.67 105531436.96 111775270.33
##  [721]   8637517.66   9897645.13  11439093.66  13271953.37  15311806.91
##  [726]  17746283.80  21540179.67  25948169.44  30202604.33  31668125.30
##  [731]  33458533.38  34732587.86   2722947.88   3127436.17   3624300.87
##  [736]   4264106.73   5035541.02   5948802.12   7093917.95   8277416.29
##  [741]   8932825.32  10389389.62  12003103.36  13752054.53   1478683.14
##  [746]   1441909.54   1418315.80   1453877.78   1516965.39   1641525.49
##  [751]   1746309.16   1776886.43   1787659.91   1845877.47   1956616.02
##  [756]   2074881.00    812500.26    974893.14   1159004.82   1350989.37
##  [761]   1554339.97   1754612.31   1936894.01   2110135.24   2477300.76
##  [766]   2776141.80   3025717.30   3226101.14  23835465.70  24594124.33
##  [771]  25425721.79  26338561.20  27188916.64  28036750.49  28276086.74
##  [776]  28374455.12  28431430.32  28752072.01  28977483.55  29088151.36
##  [781]    714496.77    769923.26    835187.05    933610.35   1002524.94
##  [786]   1081732.10   1152188.53   1166478.62   1193011.46   1269216.46
##  [791]   1335826.89   1393726.44  43231120.98  45783663.35  47919166.82
##  [796]  50417563.39  53601525.89  56944541.69  59237179.05  61056850.47
##  [801]  62178046.95  62992657.79  63547222.80  63749814.03    304730.45
##  [806]    374222.54    467953.50    628899.90    807830.93    970252.18
##  [811]   1175596.21   1412245.34   1935420.30   2264940.19   2655657.46
##  [816]   3028856.23   3232449.77   3727861.72   4339726.98   5096284.37
##  [821]   6023003.68   7250835.81   8831400.11  10599721.97  12510940.46
##  [826]  14132593.74  15694064.76  17805820.12   4433288.14   4706476.07
##  [831]   5459557.85   6309576.27   7392471.31   8164713.15   8825812.26
##  [836]   9535830.25  10357550.53  10793397.88  11108505.88  11651659.03
##  [841]  10474300.80  11306519.80  13210921.67  15066514.61  16754015.44
##  [846]  18220328.61  19665811.47  20815266.54  21908777.14  23094904.76
##  [851]  23994191.99  24534069.07    134191.18    163184.57    226862.06
##  [856]    327948.94    475640.93    599811.24    764424.02    959802.71
##  [861]    726513.96    902822.81   1073335.55   1276432.99    722181.90
##  [866]    826750.89    946281.28   1096450.49   1343752.19   1562223.35
##  [871]   1547258.26   1547365.05   1613442.40   1719571.48   1843546.97
##  [876]   1965869.53    374522.92    406837.00    446777.40    498439.32
##  [881]    558637.79    626134.68    706302.13    799987.00    902086.24
##  [886]    992004.57   1024023.59   1007109.17    431941.79    488285.48
##  [891]    556715.10    640059.80    741715.50    852128.66    978723.60
##  [896]   1134960.06    956805.31   1100667.09   1407591.24   1597178.25
##  [901]    511058.27    602513.14    724310.02    888998.38   1102444.25
##  [906]   1371867.11   1680719.14   1905807.79   2187070.57   2384568.72
##  [911]   2689059.84   3024485.75   2382177.51   2591634.10   2852483.69
##  [916]   3168095.02   3542089.28   4004355.11   4586389.94   5284898.72
##  [921]   6105717.84   7083050.15   8237185.82   9584349.39   1459085.58
##  [926]   1610827.18   1814517.95   2073873.76   2365790.81   2818954.61
##  [931]   3251728.90   3912691.26   5007406.10   5210341.64   5912580.21
##  [936]   6663919.17   3375104.57   3870522.53   4454210.94   5078577.87
##  [941]   5722155.55   6424604.46   7223418.18   8168517.40   9163389.96
##  [946]  10243111.95  11336285.99  12416868.83   1919310.17   2121187.19
##  [951]   2345434.09   2606480.50   2914369.68   3246167.70   3499437.01
##  [956]   3817346.09   4208477.01   4692887.13   5290563.70   6016418.79
##  [961]    511649.56    538849.06    573906.45    615981.57    667186.43
##  [966]    729092.75    811808.58    921330.80   1060413.18   1223112.07
##  [971]   1415218.51   1635934.08    259261.98    305925.02    351772.53
##  [976]    395892.19    426954.74    458367.99    497864.02    523723.29
##  [981]    551130.13    578621.85    604613.91    630919.50  15073897.56
##  [986]  17509839.77  20563033.30  24000656.87  27995551.70  31883825.46
##  [991]  35825257.57  40065590.08  44060251.19  47952456.65  51245334.72
##  [996]  54356434.29    400724.78    441523.33    505668.18    575363.02
## [1001]    660960.87    764823.76    879016.30   1008735.50   1157293.70
## [1006]   1248352.63   1338187.37   1438611.39    208240.79    223255.63
## [1011]    239588.80    253471.43    267728.21    284834.46    286885.29
## [1016]    290602.76    314312.17    349558.31    363393.60    346994.95
## [1021]   4970452.60   5703996.00   6529085.18   7386003.52   8331300.10
## [1026]   9199655.81  10100716.31  11495076.02  12900593.52  14266241.55
## [1031]  15585520.75  16880497.59   3223392.26   3519265.29   3894750.34
## [1036]   4340737.83   4905160.46   5564185.16   6293842.61   6446170.94
## [1041]   6580570.95   8301903.17   9237206.81   9976239.84  10046663.50
## [1046]  10866097.00  11817412.00  12935310.00  14233373.50  15764229.00
## [1051]  17340433.00  19014481.50  20273442.50  21624141.00  22799346.00
## [1056]  23881462.00    244127.39    275350.72    312282.61    355216.85
## [1061]    412764.04    490451.24    551600.55    640938.87    779028.77
## [1066]    889332.76    988112.66   1029945.53   4591540.93   4841467.97
## [1071]   5166354.70   5631183.22   6206633.89   6966946.06   7898516.19
## [1076]   8958977.82  10163553.37  11501061.95  12937487.10  14451440.68
## [1081]   5195464.79   5518829.60   5909239.92   6306092.63   6674334.37
## [1086]   6937099.03   7165900.23   7344464.66   7600517.47   7817355.07
## [1091]   8078277.38   8303705.47   1002675.29   1120827.20   1250862.84
## [1096]   1371306.96   1472573.02   1590566.86   1614141.21   1668086.60
## [1101]   1728018.66   1848618.71   1965613.40   2070478.00    584451.18
## [1106]    681142.71    797115.68    935066.70   1093798.30   1280042.19
## [1111]   1491446.67   1673654.49   2010054.58   2305912.51   2574661.27
## [1116]   2839052.66   1690114.94   1846509.76   2038502.88   2267558.19
## [1121]   2530608.10   2841447.45   3219048.86   3666653.15   4196699.59
## [1126]   4833416.15   5570628.04   6447742.34  16560086.64  18587220.30
## [1131]  20936250.96  23644383.26  26870891.69  31105577.48  36520476.49
## [1136]  40776452.51  46682931.92  53104731.97  59951444.64  67516588.99
## [1141]   1668911.71   1751795.99   1826184.70   1901190.44   1975984.53
## [1146]   2033258.17   2070542.82   2108843.99   2160161.33   2223477.58
## [1151]   2290137.49   2338641.60    254830.62    282109.87    315544.32
## [1156]    359747.97    419834.02    508190.67    657001.40    805998.61
## [1161]    966912.35   1151668.53   1366618.42   1613606.60  20673622.30
## [1166]  23340345.54  26550737.17  30321420.70  34663485.47  39076930.96
## [1171]  45731765.71  52594292.84  60033487.91  67783441.68  76702808.36
## [1176]  84636611.47    471280.19    533233.90    609630.77    704953.50
## [1181]    810874.12    922566.96   1021657.30   1130336.89   1245807.87
## [1186]   1370822.35   1499115.52   1625991.09    778914.15    886474.08
## [1191]   1005980.51   1145142.19   1308313.67   1493871.19   1685348.75
## [1196]   1945255.44   2244070.71   2579185.20   2944137.34   3335659.92
## [1201]   4014729.26   4575172.63   5260728.52   6068994.05   6980318.91
## [1206]   7998190.15   9065781.75  10101142.47  11217447.69  12376980.17
## [1211]  13387672.51  14341082.95  11219981.94  13036870.97  15163456.78
## [1216]  17679207.06  20426065.19  23426667.60  26729688.64  30009988.82
## [1221]  33594022.66  37507762.27  41498869.46  45540238.74  12867290.16
## [1226]  14120040.13  15167477.88  15895967.58  16523775.75  17315381.07
## [1231]  18117916.27  18874896.18  19189217.94  19332558.29  19318989.12
## [1236]  19266815.46   4264559.16   4410712.29   4512263.98   4554680.76
## [1241]   4489736.12   4836386.24   4935701.92   4964164.15   4971943.63
## [1246]   5087028.02   5226918.95   5331672.82   1115040.98   1131953.58
## [1251]   1226577.17   1327945.14   1428127.52   1545299.26   1644665.99
## [1256]   1728374.67   1799908.79   1888214.72   1939230.80   1980909.85
## [1261]    130209.44    155734.73    181036.86    209022.59    233340.33
## [1266]    248207.40    261538.61    283669.19    314146.13    345440.97
## [1271]    375148.58    402882.06   8316572.31   8916635.19   9342728.00
## [1276]   9645642.43  10335329.71  10833976.70  11183165.66  11348033.64
## [1281]  11401812.70  11284902.27  11206111.18  11143432.24   1267710.16
## [1286]   1411311.14   1525919.74   1725794.98   1996355.79   2328871.04
## [1291]   2754223.29   3175106.50   3645470.03   3606586.47   3926593.33
## [1296]   4430725.54     30445.29     31092.87     33208.28     36085.92
## [1301]     39063.99     44266.78     50241.61     56164.26     63669.89
## [1306]     73473.54     85862.55    100588.72   2006068.28   2213903.80
## [1311]   2477327.71   2817550.52   3248796.71   4081336.38   5644182.59
## [1316]   7320471.63   8485349.31  10625172.85  12260272.27  13811346.42
## [1321]   1378519.68   1528057.33   1715948.99   1983726.70   2295146.86
## [1326]   2631208.38   3074650.74   3586394.36   4154643.95   4768353.18
## [1331]   5435778.32   6134602.74   3431864.23   3638058.05   3811174.81
## [1336]   3989606.85   4161905.03   4349673.83   4524002.55   4623326.94
## [1341]   4917861.03   5172254.16   5059397.54   5080025.77   1072064.39
## [1346]   1148341.24   1234505.82   1331698.02   1440183.38   1571122.64
## [1351]   1732993.51   1935099.72   2130976.35   2289393.32   2679895.74
## [1356]   3072712.27    564657.57    724386.05    876937.37    991288.71
## [1361]   1080498.88   1168255.04   1333519.08   1406706.77   1630317.45
## [1366]   1917914.24   2116899.55   2300076.09   1781605.83   1925185.13
## [1371]   2122432.55   2225325.45   2301553.58   2419362.83   2529695.77
## [1376]   2605677.63   2656193.23   2697568.12   2711845.39   2733090.16
## [1381]    746866.52    769466.14    795182.15    828158.74    853446.74
## [1386]    881098.02    939559.36    982274.27   1006712.36   1014386.55
## [1391]   1016078.51   1017506.63   1264064.87   1390836.57   1540761.24
## [1396]   1715061.87   1920707.79   2177558.50   2915034.40   3461475.62
## [1401]   3050362.98   3317222.30   3877096.04   4559849.57   7134830.15
## [1406]   8078518.05   9181212.86  10502217.74  11971787.98  13568980.33
## [1411]  15574298.63  17970602.41  19985692.03  21421242.09  22220666.47
## [1416]  22003548.83  14276852.02  14923089.40  15581877.42  16429134.26
## [1421]  17261899.88  18226118.46  18998618.08  19448233.49  19784020.53
## [1426]  19937943.65  20088676.24  20238506.03   3991712.77   4564809.27
## [1431]   5211505.24   5869265.76   6508973.20   7059092.39   7705899.54
## [1436]   8248590.38   8794606.87   9350659.74   9789899.19  10191104.55
## [1441]   4253141.50   4877581.17   5592593.30   6358908.50   7299339.33
## [1446]   8553594.49  10184474.27  12363733.91  14114540.10  16081180.61
## [1451]  18546145.70  21147765.70    145695.69    163992.85    185931.09
## [1456]    211651.55    241734.92    277603.21    326898.19    391666.42
## [1461]    482948.51    529181.38    567198.56    568789.74   3566600.42
## [1466]   3686856.94   3786958.72   3941594.65   4070062.51   4135251.86
## [1471]   4172963.69   4222494.96   4371373.51   4461442.80   4491758.32
## [1476]   4532473.87   2414867.12   2571954.74   2843215.55   3042983.07
## [1481]   3214297.56   3171703.15   3248261.86   3340111.85   3513659.27
## [1486]   3612948.16   3698118.98   3796083.71   1831596.24   2076012.62
## [1491]   2418407.02   2841346.96   3351871.71   3967849.24   4707127.92
## [1496]   5622981.89   6611201.27   7542515.12   8579952.46   9659465.77
## [1501]   4275784.47   5082861.43   5960380.44   6825667.93   7615050.76
## [1506]   8395396.26   9254408.18   9884426.78  10351066.83  10824405.91
## [1511]  11238737.21  11601506.14   4161820.83   4726762.27   5432340.00
## [1516]   6304080.11   7353754.49   8565263.75   9922628.12  11520730.91
## [1521]  13303149.34  15343839.09  17297339.04  19070373.74  10645079.90
## [1526]  12521355.29  14632199.60  17012772.23  19638838.68  22075123.11
## [1531]  24414776.61  26456662.33  28335855.95  30111264.81  31406330.59
## [1536]  32537803.70    609986.40    679185.45    764582.77    868513.80
## [1541]   1029000.33   1155057.39   1323054.79   1577733.10   1874293.65
## [1546]   2160936.14   2489132.11   2851230.98    332936.64    384500.20
## [1551]    446247.76    482888.18    490909.28    523454.28    562799.26
## [1556]    599362.30    595520.00    573446.79    556646.30    537308.25
## [1561]   1824601.74   1976122.12   2144106.15   2394459.18   2653130.14
## [1566]   3004090.94   3368829.12   3864393.21   4263704.86   4618272.90
## [1571]   4888148.95   5141625.46  11118823.05  12836578.88  14895508.93
## [1576]  16707071.68  18748201.85  21204151.06  23666516.18  26443208.52
## [1581]  29092411.17  31527124.21  33657718.04  35583552.64   2912765.88
## [1586]   3338137.69   3844782.14   4450601.46   5095617.87   5729300.87
## [1591]   6470041.13   7641833.86   9126417.09  10605535.28  12370398.36
## [1596]  14585727.19  25219989.75  25720641.59  26652238.59  27486571.43
## [1601]  28047447.56  28098214.37  28178968.21  28501642.39  28944527.05
## [1606]  29417170.27  29970955.00  30404720.63  78783495.24  85999423.56
## [1611]  93277086.57  99365765.18 104958903.02 110131536.32 116106422.28
## [1616] 121416708.68 128463096.47 136473763.72 143857311.55 150591449.33
## [1621]   1129340.88   1215554.89   1302034.68   1377011.81   1417614.70
## [1626]   1440012.17   1480458.61   1526302.70   1578699.50   1636034.12
## [1631]   1685406.00   1729053.73   2723628.90   3356235.23   4075898.99
## [1636]   4859546.74   5763077.13   6758353.48   7815959.21   8960032.79
## [1641]  10138148.46  11192281.75  12148137.52  13048038.90  13123722.03
## [1646]  14499609.64  16898456.02  19732273.56  22327856.75  25267109.77
## [1651]  28071444.12  31413655.90  34970858.51  38025190.95  40454955.73
## [1656]  42632398.79    516050.30    536133.03    567666.48    572642.86
## [1661]    546352.70    632386.92    715106.02    848158.60   1055398.33
## [1666]   1416578.33   1697046.74   2010678.67   2482305.36   2749447.42
## [1671]   3060453.31   3370823.72   3704170.02   4202909.88   4829797.78
## [1676]   5610655.87   6684938.25   7914307.24   9351745.91  11107011.88
## [1681]   1336573.69   1508655.98   1711226.36   1950888.54   2254135.25
## [1686]   2609069.34   3050907.84   3636809.66   4191186.94   4709430.18
## [1691]   5298441.31   5873653.11   1540656.94   1823429.38   2139131.64
## [1696]   2498000.90   2930967.18   3321396.29   3818656.43   4608562.08
## [1701]   5352516.71   5702870.22   5963617.52   6155806.35

NOTE: Here 1 denotes it’s calculating mean on rows

tapply also works as aggregate but differs in output format
tapply(gm.subset$gdpPercap, gm.subset$country, FUN= mean) -> tapplycheck
str(tapplycheck)
##  num [1:142(1d)] 851 5271 5756 3785 10789 ...
##  - attr(*, "dimnames")=List of 1
##   ..$ : chr [1:142] "Afghanistan" "Albania" "Algeria" "Angola" ...

Q: Difference between aggregate and tapply?

ANs: aggregate gives vector as an output whereas tapply gives list in output.