Bài 1

A <- matrix(c(1, 1, 3, 5, 2, 6, -2, -1, -3), nrow = 3, byrow = TRUE)
A
##      [,1] [,2] [,3]
## [1,]    1    1    3
## [2,]    5    2    6
## [3,]   -2   -1   -3
#a
A_cubed = A%*%A%*%A
A_cubed
##      [,1] [,2] [,3]
## [1,]    0    0    0
## [2,]    0    0    0
## [3,]    0    0    0
#b
sum_col <- A[, 1] + A[, 2]
A<- cbind(A[, 1:2], sum_col)
A
##            sum_col
## [1,]  1  1       2
## [2,]  5  2       7
## [3,] -2 -1      -3

Bài2

x <- 0:4
y <- 0:4
result <- outer(x, y, "+")
result
##      [,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ài3

B<- 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)
y <- c(7, -1, -3, 5, 17)
x <- solve(B, y)
print(x)
## [1] -2  3  5  2 -4

Bài 4

result <- 0

for (i in 1:20) {
  for (j in 1:5) {
    term <- i^4 / (3 + j)
    result <- result + term
  }
}
result
## [1] 639215.3

bài 5

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(gapminder)
library(ggplot2)
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
data("gapminder")

# Lọc dữ liệu dành cho Việt Nam và đặt tên tập dữ liệu là vietnamdata
vietnamdata <- filter(gapminder, country == "Vietnam")

# Sử dụng dplyr và toán tử pipe để tính tuổi thọ trung bình của người Việt Nam qua các năm
tuoithoTB <- vietnamdata %>%
  group_by(year) %>%
  summarize(tuoithoTB = mean(lifeExp))

print(tuoithoTB)
## # A tibble: 12 × 2
##     year tuoithoTB
##    <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
# Vẽ biểu đồ so sánh tổng thu nhập GDP của các nước tại châu Á và châu Âu vào năm 2007
#Châu á
gapminder %>%
  filter(year == 2007, continent == "Asia") -> Asia2007

ggplot(data = Asia2007, mapping = aes(x= reorder(country, gdpPercap), y = gdpPercap, fill = country))+
  geom_bar(stat = "identity", width = 0.9)+
  coord_flip()+
  theme(legend.position = "none")+
  labs(x="", y="GDP trung bình Châu á 2007",
       title = "biểu đồ GDPtrung bình châu á 2007")->graph1
#Châu Âu
gapminder %>%
  filter(year == 2007, continent == "Europe") -> Europe2007

ggplot(data = Europe2007, mapping = aes(x= reorder(country, gdpPercap), y = gdpPercap, fill = country))+
  geom_bar(stat = "identity", width = 0.9)+
  coord_flip()+
  theme(legend.position = "none")+
  labs(x="", y="GDP trung bình Châu Âu 2007",
       title = "biểu đồ GDP trung bình châu Âu 2007")->graph2


grid.arrange(graph1, graph2, ncol= 2)

#Nhận xét Từ biểu đồ ta thấy GDP của Châu Âu cao hơn Châu Á rất nhiều, mặc dù Châu Á đang phất triển nhưng vẫn có những quốc gia phát triển rất nhanh như là: Singapore, Kuwait… Hơn 1 nửa quốc gia của Châu Á có mức GDP thấp hơn 10000 mà của Châu Âu chỉ có vài nước