#BÀI KIỂM TRA SỐ 2

Bài 1

A = matrix(c(1, 5, -2, 1, 2, -1, 3, 6, -3), nrow = 3, ncol = 3)
A
##      [,1] [,2] [,3]
## [1,]    1    1    3
## [2,]    5    2    6
## [3,]   -2   -1   -3
#a
A1 = A %*% A %*% A
A1
##      [,1] [,2] [,3]
## [1,]    0    0    0
## [2,]    0    0    0
## [3,]    0    0    0
#b
B <- A[,1] + A[,2]
cbind(A[,1:2],B)
##             B
## [1,]  1  1  2
## [2,]  5  2  7
## [3,] -2 -1 -3

##Bài 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

##Bài 3

A = matrix(c(1,2,3,4,5,
             2,1,2,3,4,
             3,2,1,2,3,
             4,3,2,1,2,
             5,4,3,2,1),
           nrow = 5, ncol = 5, byrow = TRUE)
A
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    2    3    4    5
## [2,]    2    1    2    3    4
## [3,]    3    2    1    2    3
## [4,]    4    3    2    1    2
## [5,]    5    4    3    2    1
y <- c(7,-1,-3,5,17)
x <- solve(A,y)
x
## [1] -2  3  5  2 -4

##Bài 4

sum((1:20)^4) * sum(1/(4:8))
## [1] 639215.3

##Bài 5

library(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
data("gapminder")
#ý a
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.
#ý b
gapminder %>%
  filter(country == "Vietnam") %>% 
  group_by(year) %>%
    summarize(trungbinh_tuoitho =mean(lifeExp))
## # A tibble: 12 × 2
##     year trungbinh_tuoitho
##    <int>             <dbl>
##  1  1952              40.4
##  2  1957              42.9
##  3  1962              45.4
##  4  1967              47.8
##  5  1972              50.3
##  6  1977              55.8
##  7  1982              58.8
##  8  1987              62.8
##  9  1992              67.7
## 10  1997              70.7
## 11  2002              73.0
## 12  2007              74.2
#ý c
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
library(ggplot2)
#lọc dữ liệu vào năm 2007
gapminder %>%
  filter(year==2007 & continent =="Asia") %>%
  select(country, gdpPercap) -> ChauA

gapminder %>%
  filter(year==2007 & continent =="Europe") %>%
  select(country, gdpPercap)->ChauAu

ggplot(ChauA, aes(x = reorder(country, -gdpPercap), y = gdpPercap)) +
  geom_bar(stat = "identity", fill = "green", color= "black") +
  coord_flip() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(x = "Country", y = "GDP", title = "GDP Châu Á (2007)") -> plot_1

 ggplot(ChauAu, aes(x = reorder(country, -gdpPercap), y = gdpPercap)) +
  geom_bar(stat = "identity", fill = "orange", color = "black") +
  coord_flip() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(x = "Country", y = "GDP", title = "GDP Châu Âu (2007)") -> plot_2
 
grid.arrange(plot_1, plot_2, ncol = 2)