#Ho van ten: Pham The Hung
#Bai 1
##a kiem tra matrix A = matrix A^3
a <- c(1, 1, 3)
b <- c(5, 2, 6)
c <- c(-2, -1, -3)
matrix <- rbind(a,b,c)
matrix 
##   [,1] [,2] [,3]
## a    1    1    3
## b    5    2    6
## c   -2   -1   -3
matrix3 <- matrix %*% matrix %*% matrix
matrix3
##   [,1] [,2] [,3]
## a    0    0    0
## b    0    0    0
## c    0    0    0
##b thay cot 3 cua matrix a = tong cot thu 1 va cot thu 2 matrix a
d <- a + b
d
## [1] 6 3 9
matrix <- rbind(a,b,d)
matrix
##   [,1] [,2] [,3]
## a    1    1    3
## b    5    2    6
## d    6    3    9
#Bai 2
x <- c(0,1,2,3,4)
y <- c(0,1,2,3,4)
outer(x,y,FUN = "+")
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    0    1    2    3    4
## [2,]    1    2    3    4    5
## [3,]    2    3    4    5    6
## [4,]    3    4    5    6    7
## [5,]    4    5    6    7    8
#Bai 3
a <- c(1, 2, 3, 4, 5)
b <- c(2, 1, 2, 3, 4)
c <- c(3, 2, 1, 2, 3)
d <- c(4, 3, 2, 1, 2)
e <- c(5, 4, 3, 2, 1)
kq <- c(7, -1, -3, 5, 15)
matrix <- rbind(a,b,c,d,e)
solve(matrix,kq)
## [1] -2.166667  3.000000  5.000000  1.000000 -3.166667
#Bai 4
count <- 0
for(i in 1:20)
  for(j in 1:5)
    count <- count + (1^4 / (3+j))
count
## [1] 17.69048
#Bai 5
#install.packages(c("dplyr","ggplot2","gapminder"))
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(gapminder)
data("gapminder")
##y 1
filter(gapminder,country == "Vietnam") -> vietnamdata
vietnamdata
## # A tibble: 12 × 6
##    country continent  year lifeExp      pop gdpPercap
##    <fct>   <fct>     <int>   <dbl>    <int>     <dbl>
##  1 Vietnam Asia       1952    40.4 26246839      605.
##  2 Vietnam Asia       1957    42.9 28998543      676.
##  3 Vietnam Asia       1962    45.4 33796140      772.
##  4 Vietnam Asia       1967    47.8 39463910      637.
##  5 Vietnam Asia       1972    50.3 44655014      700.
##  6 Vietnam Asia       1977    55.8 50533506      714.
##  7 Vietnam Asia       1982    58.8 56142181      707.
##  8 Vietnam Asia       1987    62.8 62826491      821.
##  9 Vietnam Asia       1992    67.7 69940728      989.
## 10 Vietnam Asia       1997    70.7 76048996     1386.
## 11 Vietnam Asia       2002    73.0 80908147     1764.
## 12 Vietnam Asia       2007    74.2 85262356     2442.
##y 2
gapminder %>%
  filter(country == "Vietnam") -> data
summarise(data, meanAge = mean(lifeExp))
## # A tibble: 1 × 1
##   meanAge
##     <dbl>
## 1    57.5
##y 3
#install.packages("gridExtra")
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
gapminder %>%
  filter(continent == "Europe", year == 2007) -> chauAu
gapminder %>%
  filter(continent == "Asia", year == 2007) -> chauA
chauAu
## # A tibble: 30 × 6
##    country                continent  year lifeExp      pop gdpPercap
##    <fct>                  <fct>     <int>   <dbl>    <int>     <dbl>
##  1 Albania                Europe     2007    76.4  3600523     5937.
##  2 Austria                Europe     2007    79.8  8199783    36126.
##  3 Belgium                Europe     2007    79.4 10392226    33693.
##  4 Bosnia and Herzegovina Europe     2007    74.9  4552198     7446.
##  5 Bulgaria               Europe     2007    73.0  7322858    10681.
##  6 Croatia                Europe     2007    75.7  4493312    14619.
##  7 Czech Republic         Europe     2007    76.5 10228744    22833.
##  8 Denmark                Europe     2007    78.3  5468120    35278.
##  9 Finland                Europe     2007    79.3  5238460    33207.
## 10 France                 Europe     2007    80.7 61083916    30470.
## # ℹ 20 more rows
chauA
## # A tibble: 33 × 6
##    country          continent  year lifeExp        pop gdpPercap
##    <fct>            <fct>     <int>   <dbl>      <int>     <dbl>
##  1 Afghanistan      Asia       2007    43.8   31889923      975.
##  2 Bahrain          Asia       2007    75.6     708573    29796.
##  3 Bangladesh       Asia       2007    64.1  150448339     1391.
##  4 Cambodia         Asia       2007    59.7   14131858     1714.
##  5 China            Asia       2007    73.0 1318683096     4959.
##  6 Hong Kong, China Asia       2007    82.2    6980412    39725.
##  7 India            Asia       2007    64.7 1110396331     2452.
##  8 Indonesia        Asia       2007    70.6  223547000     3541.
##  9 Iran             Asia       2007    71.0   69453570    11606.
## 10 Iraq             Asia       2007    59.5   27499638     4471.
## # ℹ 23 more rows
ggplot(data = chauAu, mapping = aes(x = reorder(country,gdpPercap), 
                                    y = gdpPercap, fill = country)) +
  geom_bar(stat = "identity") +
  coord_flip() +
  theme(legend.position = "none") -> gdpEurope
ggplot(data = chauA, mapping = aes(x = reorder(country,gdpPercap), 
                                    y = gdpPercap, fill = country)) +
  geom_bar(stat = "identity") +
  coord_flip() +
  theme(legend.position = "none") -> gdpAsian
gdpEurope

gdpAsian