Give an introduction to section 1: Setting up function
Viet ham tinh dien tich cua mot hinh chu nhat. Code chunk ’’‘{r}’’’
rm(list = ls())
library(dplyr)
library(readr)
dien_tich_hcn <- function(a, b) {
result <- a*b
print(result)
}
dien_tich_hcn(3, 7)## [1] 21
Tinh sample standard deviation cua 1 vector numeric
tinh_std_deviation <- function(a) {
n <- length(a)
result <- sqrt(sum((a-mean(a))^2/(n-1)))
print(result)
}
a <- c(2, 6, 7, 13, 19)
tinh_std_deviation(a)## [1] 6.655825
Give an introduction to section 2: WOrking with stock dataset
Data type of the dataset: character
all_data_path <- dir("D:/Intro_to_R_n_Python_May2020/Intro_to_R/stock/stock", full.names = TRUE)
typeof(all_data_path)## [1] "character"
## [1] "character"
Co ton tai file du lieu nao co 3 chu cai vnm khong?
list.files(path = "D:/Intro_to_R_n_Python_May2020/Intro_to_R/stock/stock", recursive = TRUE, pattern = "vnm") -> vnm_file
vnm_file## [1] "excel_vnm.csv"
Co bao nhieu file du lieu co ky hieu ^
test <- "\\^" #special expression, deactivate by \\
grep(test, all_data_path) -> whichfile
length(whichfile)## [1] 2
Đọc hai files dữ liệu lần lượt có các cụm từ vnm và fpt rồi sử dụng lệnh bind_rows() để join hai bộ dữ liệu này thành một data frame duy nhất
library(readr)
library(dplyr)
library(purrr)
key1 = "vnm"
key2 = "fpt"
#key <- c(key1, key2)
vnm_files <- dir("D:/Intro_to_R_n_Python_May2020/Intro_to_R/stock/stock", full.names = TRUE, pattern = key1)
fpt_files <- dir("D:/Intro_to_R_n_Python_May2020/Intro_to_R/stock/stock", full.names = TRUE, pattern = key2)
selected_files <- c(vnm_files, fpt_files)
data <- selected_files %>%
map(read_csv) %>%
bind_rows()
data #merged files of VNM and FPT## # A tibble: 5,510 x 14
## `<Ticker>` `<DTYYYYMMDD>` `<OpenFixed>` `<HighFixed>` `<LowFixed>`
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 VNM 20170731 153 153 152.
## 2 VNM 20170728 153. 153. 153.
## 3 VNM 20170727 153 153 153.
## 4 VNM 20170726 152 154. 152.
## 5 VNM 20170725 152. 152 151.
## 6 VNM 20170724 151. 152 150
## 7 VNM 20170721 151. 152 151.
## 8 VNM 20170720 152 153. 152.
## 9 VNM 20170719 153 154. 153.
## 10 VNM 20170718 152 153 151
## # ... with 5,500 more rows, and 9 more variables: `<CloseFixed>` <dbl>,
## # `<Volume>` <dbl>, `<Open>` <dbl>, `<High>` <dbl>, `<Low>` <dbl>,
## # `<Close>` <dbl>, `<VolumeDeal>` <dbl>, `<VolumeFB>` <dbl>,
## # `<VolumeFS>` <dbl>
Lặp lại công việc ở câu hỏi 4 nhưng cho tất cả các files dữ liệu có trong thư mục stock.
Đọc hai files dữ liệu lần lượt có các cụm từ vnm và fpt rồi sử dụng lệnh bind_rows() để join hai bộ dữ liệu này thành một data frame duy nhất
library(ggplot2)
iris %>%
group_by(Species) %>%
count() %>%
ggplot(aes(Species, n, fill = Species)) +
geom_col()Đọc hai files dữ liệu lần lượt có các cụm từ vnm và fpt rồi sử dụng lệnh bind_rows() để join hai bộ dữ liệu này thành một data frame duy nhất
library(ggplot2)
iris %>%
group_by(Species) %>%
count() %>%
ggplot(aes(Species, n, fill = Species)) +
geom_col()