# 1:2020
seq(from = 1, to = 20, length.out = 20 )
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
20:1
##  [1] 20 19 18 17 16 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1
#seq(from = 20, to = 1, length.out = 20 )
c(1:20,19:1)
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 19 18 17 16 15
## [26] 14 13 12 11 10  9  8  7  6  5  4  3  2  1
tmp = c(4, 6, 3)
#tmp
e = rep(tmp, times = 10)
#e
f =  rep(tmp,l=31)
f
##  [1] 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4
 rep(tmp,l=31)
##  [1] 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4

Bài 2

set.seed(50)
xVec <- sample(0:999, 250, replace=T)
yVec <- sample(0:999, 250, replace=T)

# a)
yVec[-1] - xVec[-length(xVec)]
##   [1] -359  692 -724   40 -626 -719 -809  527  -89 -829  248  144 -749 -352 -220
##  [16] -249  387 -492   85 -106  303  -97 -436  146  282 -206 -385  -96 -567 -757
##  [31]  287  277 -562  292  -89  -93 -847 -822 -203  679  309 -199 -273    4  -47
##  [46]  142  122  414 -602 -304 -674   -8 -662 -168 -349  -63 -221  115    1 -600
##  [61] -382 -487    2  375   19 -113 -634  107   60   47  214 -325  -49 -290  169
##  [76]  290 -624  457 -408  581 -189  204  -80  409  209 -410  461   37 -127  185
##  [91]  382 -446   44  -56 -270 -598 -378 -155  134 -187  109  316 -139  158  305
## [106]  -39 -119  182  441 -403 -107  615  614 -378 -464   31 -385  665  674 -217
## [121] -279 -406  -45 -489 -350 -451  -18  660  504   -6   60 -130 -379 -302 -219
## [136]  -21  438  129 -201 -275  131  694  -96 -176  117 -113  887 -439 -126 -148
## [151]  392 -158  444 -291  232  -12 -274  477 -510  336 -759 -363 -195 -220  160
## [166] -308 -333  302 -183  227  -12  428  665 -301   -8  222  -50 -444 -425 -650
## [181] -424  318  154  238 -727   71  472  908  265  654 -644 -754  657 -382 -313
## [196]  910 -381  394 -596  602  397 -572  378 -274 -271  601 -791 -378 -461   39
## [211]  163 -118 -332 -170  -94  262 -474  566 -273 -366 -400  374   42  100  135
## [226]  609 -527  580 -219  128 -524  620 -206  410 -280  -66  -50  252  279   48
## [241] -595  -59 -623  247  514   62 -102  475  287
# b)
sin(yVec[-length(yVec)]) / cos(xVec[-1])
##   [1]  -0.251172387  -1.714506221  -1.020534796   1.903299501   4.119691628
##   [6]   0.474814101   0.989633106   1.157740470  -2.882161424   1.394557843
##  [11]   3.580223541  -0.664006287   0.847013135  -1.095954285   0.469790086
##  [16]  -0.544605141  -1.253304942  -0.353697082  -1.357919064  -1.271051804
##  [21]   0.009344711 -10.576799813  -0.674196809  -2.087577737 -32.238631575
##  [26]  -4.966988605  -0.490160550  -0.043753175  -1.766774293  -2.456887042
##  [31]  -1.034891510  -0.118782199   0.036938122  -0.173612680   0.851649079
##  [36]  -0.353501116  -1.024989992   0.589100687   4.018304788  -1.004929483
##  [41]   0.977012057   0.952499185  -0.553614203   1.115864382   1.254898524
##  [46]   1.155274507  -1.032061497   0.906767318  -0.945760017   2.658291106
##  [51]  -1.254932608  -0.547511538   3.928248333  -3.673653649  -0.127796877
##  [56]  -3.226308105  -0.894400173   0.701414603   2.128023689  -0.414254810
##  [61]  -0.917829147   0.558286542   0.000000000  -0.564183939   0.946409854
##  [66]   1.407199013   0.800206658  -1.146531865   1.181638746   0.495187680
##  [71]   1.446378081  -1.004546310  -0.648760582  -0.909533924   2.364794213
##  [76]  -1.067542299   0.735761986  -1.085930368   1.046185000  -0.235199131
##  [81]   5.984225913   0.811049413   1.134803403   0.652427664  -0.915236455
##  [86]   2.180704313   1.832739489  -0.433287348  -2.250658142  -1.675524524
##  [91]  -1.553949156  -1.249207462  12.092176300   0.808459235   0.832313214
##  [96]   0.903245844   1.066231524   5.294081295  25.183138328  -1.825927150
## [101]   0.246533436   0.874187672   1.092740398  -0.722495528   0.766922081
## [106]   1.339270617  -1.106082115   2.130306844   0.799377104   6.004793950
## [111]   8.758581778  -0.981032578   1.256557471  -2.048399633   1.195677807
## [116]  -1.040371386   0.644501924  -3.483330739 -13.236374600   1.549790230
## [121]  31.620598675   1.501371199  -0.815852894  -0.286937344   2.086096713
## [126]   0.169201819  -0.944412409   1.404263380   1.918061130  -6.647769454
## [131]   1.097542414  22.775427858   1.581242582   0.596376118   1.286025755
## [136]  -0.274884286   1.792271243   0.929571073  -1.398639309   0.180473893
## [141]  -0.861690054  -3.817208262   1.406954246  -0.116266112   0.911847938
## [146]   1.720472285  -0.351853469  -0.140708190 102.843164547  -0.814695119
## [151]  -4.198084329  -0.796216596  -1.483961597  -0.913007027   2.209820017
## [156]  -0.874704929  -7.182835580  -0.171459039   1.010581535  -0.060316505
## [161]  -5.934493823  -0.194382721   2.788122775   7.857328199   9.813569870
## [166]  -1.041969001 -17.395410126   0.604907292  -1.027075437  -0.256821966
## [171]   0.380364801  -0.898464055  -0.755130341  -4.425456175   0.969617473
## [176]   0.292765308  -1.130607542   0.904382713  -0.969592200  -0.069832291
## [181]   0.000000000  -0.629251004   3.078476507   0.433631660   1.711984983
## [186]  -3.189462891   0.221313374  -1.594268883  -0.158597479   0.218596389
## [191]   0.984958329  -2.227888183   0.233630793  -2.913722008   0.773617325
## [196]   1.026041041   9.839208858  -1.672630213  -0.370798565   1.312355946
## [201]  -0.889637266  -1.473979138  -0.618321180  -0.117513292   0.237786925
## [206]   0.876611998   1.328547955  -0.982203606  -0.061533125   1.004160645
## [211]  -1.266243000   0.534185156   0.571802673  -0.622297532   0.580410706
## [216]  -1.482925131   1.095346759   1.701972216  -0.976847974  -1.309841610
## [221]   1.053044730   0.277103276   0.934757395  -1.112172484  -0.326263919
## [226]   0.416934946  -1.171403611  -1.014835503  -0.549469340  -0.741222996
## [231]   1.877938004  -0.074048223  -0.021532438 -11.511078788  -1.389914300
## [236]   2.423540052   0.791040396  -6.445834186  -0.523259399  -1.446820579
## [241]  -0.734282899  -1.420238075  -0.820938658  -0.949002149   1.452579737
## [246]  -2.219943144  -1.114115747   0.747424267  -0.919986986
# c)
xVec[-c(249,250)] + 2*xVec[-c(1,250)]-xVec[-c(1,2)]
##   [1] -432 1554  145  937 1811 2133  855  883 2241 1428 -681 1225 2238  842  885
##  [16]  391 1031 1982 1288  798  416 1618 1932 1055 2127 1157 1459  956 2323 1263
##  [31] -426 1014  880 -276  863 1761 2374 1379  344  434 1629 1203 2177  682   58
##  [46] 1226  815 1005 1830 2686  320 1871  700 1189 1356 2095  818 -840 1403 1790
##  [61] 1343  564  803  303 1559 2033 1126  711 1608  614  976 2108 1821  473 -281
##  [76] 1586  473 1755  263 1559   -3 1558 1209  -98  846 1131  848  778  350 -508
##  [91] 1253   37 1380 1064  799 1816 2216  816 1445 1179  931 1474  394 1219  556
## [106] 1333 1798  406 1122 1175  636  135  676 1807 1514 1387  531  154  478  408
## [121] 2011 1071 1427 1472 2076 1747  369 -343 1252  997  400  790 1398 2042  776
## [136]  689 1589  547 1510  934 -289  978 1202 -593 1834  228 1515 1275  452  979
## [151] 1210  804 1813  794  435 1688  948  914 1012 1244 1965 1456 1683  798 1463
## [166] 2673  608 1735  204 1100  713  462  352 1109 2028  401 1380 1531 1223 1660
## [181] 1373  632 -148 1321 2084 1650  112  559  147  552 2410  782  703 2550  354
## [196] 1500  368 1147  673 -268 1355 1719  506 2061  279  813 1833 1976 2057 2043
## [211] 1030 1081  963  808 -241 1331  488  470 1993 1412  454 1210 2389  835 -187
## [226] 1162 -177 2083  286 1363  478  423  308 1123   43 1458  964 1250  750 1493
## [241] 2121 2040  760  761  445 1787 1083 -306
# d)
sum(exp(-xVec[-1])/(xVec[-length(xVec)]+10))
## [1] 5.029496e-05

Bài 3

1+sum(cumprod(seq(2,38,b=2)/seq(3,39,b=2)))
## [1] 6.976346

Phần 2: Lập trình hàm trong R

Bài 1

n <- as.integer(readline("n: "))
## n:
myvector <- scan(n=n)
print(myvector)
## numeric(0)
#myvector <- c(1:10)
# Xây dựng hàm tính tổng các phần tử của một véc tơ
tinhtong <- function(x){
  tong = 0
  for(i in 1:length(x)){
    tong = tong + x[i]
  }
  return(tong)
}
# Kiểm tra hàm xây dựng
tinhtong(myvector)
## numeric(0)
# Hàm tính giá trị trung bình
trungbinh <- function(x){
  trungbinh_x <- tinhtong(x)/length(x)
  return(trungbinh_x)
}
# Test hàm
trungbinh(myvector)
## numeric(0)
# Xây dựng hàm tìm giá trị nhỏ nhất
GT_min <- function(x) {
  my_min = x[1]
  for (i in seq_along(x)) {
    if (x[i] < my_min) my_min = x[i]
  }
  return(my_min)
}
# Kiểm tra hàm
GT_min(myvector)
## [1] NA
# Xây dựng hàm tìm giá trị nhỏ nhất
GT_max <- function(x) {
  my_min = x[1]
  for (i in seq_along(x)) {
    if (x[i] > my_min) my_min = x[i]
  }
  return(my_min)
}
# Kiểm tra hàm
GT_max(myvector)
## [1] NA
#  Hàm tính phương sai của một véc tơ số
phuongsai <- function(x){
  binhphuong <- (x - trungbinh(x))^2
  total = 0
  for (i in 1:length(x)){
    total = total + binhphuong[i]
  }
  ketqua = total/(length(x)-1)
  return(ketqua)
}
# Test hàm
phuongsai(myvector)
## numeric(0)
# Xây dựng hàm tính độ lệch chuẩn
doLechChuan <- function(x){
  ketqua = sqrt(phuongsai(x))
  return(ketqua)
}
doLechChuan(myvector)
## numeric(0)

Phần 3: Tính hiệp phương sai

n <- as.integer(readline("n: "))
## n:
vectorA <- scan(n=n)
print(vectorA)
## numeric(0)
vectorB <- scan(n=n)
print(vectorB)
## numeric(0)
# Viết hàm tính hiệp phương sai covariance
covariance <- function(x, y){
  a = (x - mean(x))*(y-mean(y))
  sum = 0
  if(length(x) == length(y)){
    for (i in 1:length(x)){
      sum = sum + a[i]
    }
  }
  else {
    print("Hai véc tơ phải cùng chiều dài!")
    break
  }
  return(sum/(length(x)-1))
}
covariance(vectorA, vectorB)
## numeric(0)

Thư viện ggplot2

Lớp biến số

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.2
library(gapminder)
## Warning: package 'gapminder' was built under R version 4.2.2
mydata <- gapminder
head(gapminder)
## # A tibble: 6 × 6
##   country     continent  year lifeExp      pop gdpPercap
##   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
## 1 Afghanistan Asia       1952    28.8  8425333      779.
## 2 Afghanistan Asia       1957    30.3  9240934      821.
## 3 Afghanistan Asia       1962    32.0 10267083      853.
## 4 Afghanistan Asia       1967    34.0 11537966      836.
## 5 Afghanistan Asia       1972    36.1 13079460      740.
## 6 Afghanistan Asia       1977    38.4 14880372      786.
library(ggthemes)
## Warning: package 'ggthemes' was built under R version 4.2.2
# Chúng ta bắt đầu trực quan dữ liệu
ggplot(data = mydata, mapping = aes(x = gdpPercap, y = lifeExp)) + geom_point(aes(color = continent)) +
  geom_smooth(method = "loess") +
  scale_x_log10() +
  labs(x =" Log GDP per Capita", y = "Life Expectancy") +
  ggtitle("Association between GDP Per Capita and Life Expectancy") + theme(plot.title = element_text(lineheight = 0.8, face = "bold", hjust = 0.5)) + theme_economist()
## `geom_smooth()` using formula = 'y ~ x'

p2 <- ggplot(data = mydata, mapping = aes(x = gdpPercap, y = lifeExp))
p2 + geom_point() -> p2 
#thêm một đường biểu diễn tương quan tuyến tính
p2 + geom_smooth(method = "loess") -> p2
# trực quan theo biến lục địa
ggplot(data = mydata, mapping = aes(x = gdpPercap, y = lifeExp)) + geom_point(aes(color = continent)) +
  geom_smooth(method = "loess")
## `geom_smooth()` using formula = 'y ~ x'

#Chuyển sang đơn vị log() cho dễ nhìn
ggplot(data = mydata, mapping = aes(x = gdpPercap, y = lifeExp)) + geom_point(aes(color = continent)) +
  geom_smooth(method = "loess") +
  scale_x_log10()
## `geom_smooth()` using formula = 'y ~ x'

#Gán thêm các nhãn cho biểu đồ
ggplot(data = mydata, mapping = aes(x = gdpPercap, y = lifeExp)) + geom_point(aes(color = continent)) +
  geom_smooth(method = "loess") +
  scale_x_log10() +
  labs(x =" Log GDP per Capita", y = "Life Expectancy") +
  ggtitle("Association between GDP Per Capita and Life Expectancy") + 
  theme(plot.title = element_text(lineheight = 0.8, face = "bold", hjust = 0.5))
## `geom_smooth()` using formula = 'y ~ x'

#Thay đổi theo phong cách màu của tạp chí “The Economist” 
library(ggthemes)
ggplot(data = mydata, mapping = aes(x = gdpPercap, y = lifeExp)) + geom_point(aes(color = continent)) +
  geom_smooth(method = "loess") +
  scale_x_log10() +
  labs(x =" Log GDP per Capita", y = "Life Expectancy") +
  ggtitle("Association between GDP Per Capita and Life Expectancy") + 
  theme(plot.title = element_text(lineheight = 0.8, face = "bold", hjust = 0.5)) + theme_economist()
## `geom_smooth()` using formula = 'y ~ x'

### Một số biểu đồ khác của ggplggplot2

Biểu đồ Histogram

year2007 <- subset(mydata, year == 2007)
head(year2007)
## # A tibble: 6 × 6
##   country     continent  year lifeExp      pop gdpPercap
##   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
## 1 Afghanistan Asia       2007    43.8 31889923      975.
## 2 Albania     Europe     2007    76.4  3600523     5937.
## 3 Algeria     Africa     2007    72.3 33333216     6223.
## 4 Angola      Africa     2007    42.7 12420476     4797.
## 5 Argentina   Americas   2007    75.3 40301927    12779.
## 6 Australia   Oceania    2007    81.2 20434176    34435.
ggplot(data = year2007, mapping = aes(gdpPercap)) +
  geom_histogram(fill = "lightblue", color = "white") +
  labs(title = "Distribution of GDP per Capita in 2007")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#Nếu chúng ta không muốn trục Oy là count mà muốn là xác suất (tần suất xuất hiện)
ggplot(data = year2007, mapping = aes(gdpPercap)) +
  geom_histogram(aes(y = ..density..), fill = "lightblue", color = "white")
## Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(density)` instead.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#phân phối theo từng lục địa
ggplot(data = year2007, mapping = aes(gdpPercap, fill = continent)) +
  geom_density(alpha = 0.4)

#### Biểu đồ thanh barplot

dim(year2007)
## [1] 142   6
ggplot(data = year2007, mapping = aes(x = continent, fill = continent)) +
  geom_bar() 

#phần legend hơi thừa, xóa bớt

dim(year2007)
## [1] 142   6
ggplot(data = year2007, mapping = aes(x = continent, fill = continent)) +
  geom_bar() + theme(legend.position = "none")

#Sử dụng toán tử điều kiện [] để tạo ra một biến mới tên là level_age có tính thứ bậc
year2007$level_age[year2007$lifeExp < 60.0] <- "< 60 age"
## Warning: Unknown or uninitialised column: `level_age`.
year2007$level_age[60.0 <= year2007$lifeExp & year2007$lifeExp <= 80.0] <- "60-80 age"
year2007$level_age[year2007$lifeExp > 80.0] <- "> 80 age"
ggplot(data = year2007, aes(x = continent, fill = level_age)) +
  geom_bar()

#lọc các quốc gia có lục địa là Asian:

asia <- subset(mydata, year == 2007 & continent == "Asia")
dim(asia)
## [1] 33  6
ggplot(data = asia, mapping = aes(x = country, y = lifeExp, fill = country)) + geom_bar(stat = "identity", width = 0.9)

ggplot(data = asia, mapping = aes(x = country, y = lifeExp, fill = country)) + geom_bar(stat = "identity", width = 0.9) + coord_flip()

#chúng ta nên sắp xếp lại theo trật tự
ggplot(data = asia, mapping = aes(x = reorder(country, lifeExp), y = lifeExp, fill = country)) + geom_bar(stat = "identity", width = 0.9) + coord_flip() + theme(legend.position = "none") + labs(x="", y="Life Expectancy") -> graph1
#sử dụng gói gridExtra, biểu diễn hai đồ thị thanh cho tuổi thọ và thu nhập bình quân đầu người
library(gridExtra)
## Warning: package 'gridExtra' was built under R version 4.2.2
ggplot(data = asia, 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 Per Capita") -> graph2
grid.arrange(graph1, graph2, ncol = 2)

Biểu đồ hộp Boxplot

#Giả sử chúng ta muốn tóm tắt thu nhập bình quân năm 2007 của 142 quốc gia bằng biểu đồ hộp

ggplot(data = year2007, aes(x = continent, y = gdpPercap, fill = continent)) + geom_boxplot(alpha = 0.6)

ggplot(data = year2007, aes(x = continent, y = gdpPercap, fill = continent)) + 
  geom_boxplot(alpha = 0.6) + geom_jitter(alpha = 0.3)

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.