1 + 2 # Phép cộng
## [1] 3
(2 + 2) * 3 # Ưu tiên trong ngoặc
## [1] 12
4 / 3 # Chia ra số thực (float)
## [1] 1.333333
4 %/% 3 # Chia lấy phần nguyên (Integer division)
## [1] 1
4 %% 3 # Chia lấy phần dư (Modulo)
## [1] 1
2^2 # Lũy thừa
## [1] 4
2**2 # Lũy thừa (cách viết khác)
## [1] 4
a <- 8
b <- 10
s <- a * b
print(s)
## [1] 80
x <- 2
2 * x
## [1] 4
2 -> x # Gán ngược chiều (hợp lệ nhưng ít dùng)
(y <- "visible") # Gán và in giá trị ra màn hình cùng lúc
## [1] "visible"
“hello,” ‘world!’ print(“hello my sweetie”)
v <- 1:5 # Tạo vector v gồm: 1, 2, 3, 4, 5
x_vec <- 1:4
x_vec = x_vec**2 # Bình phương từng phần tử
sum(x_vec) # Tổng các phần tử
## [1] 30
x<-c("cho","meo", "ga", "vit")
length(x) # Độ dài vector
## [1] 4
nchar("di hoc") # Số ký tự trong chuỗi (trả về 3)
## [1] 6
v<-4:20
v[1] # Lấy phần tử đầu tiên
## [1] 4
v[1:3] # Lấy từ vị trí 1 đến 3
## [1] 4 5 6
v[c(1, 3, 5)] # Lấy các vị trí cụ thể
## [1] 4 6 8
v[v %% 2 == 0] # Lọc (Filter): Chỉ lấy số chẵn
## [1] 4 6 8 10 12 14 16 18 20
v[-(1:3)] # Loại bỏ các phần tử từ vị trí 1 đến 3
## [1] 7 8 9 10 11 12 13 14 15 16 17 18 19 20
v_named <- c("A" = 1, "B" = 2, "C" = 3)
v_named["A"] # Truy cập bằng tên
## A
## 1
names(v_named) <- c("x", "y", "z") # Đổi tên các phần tử
square <- function(x) x^2
square(1:4)
## [1] 1 4 9 16
square_and_subtract <- function(x, y) {
squared <- x^2 # Biến cục bộ
squared - y # Giá trị trả về (dòng cuối cùng)
}
square_and_subtract(1:5, rev(1:5))
## [1] -4 0 6 14 24
average<-7:10
average <- function(x) sum(x) / length(x)
if (2 > 3) "false" else "true"
## [1] "true"
if (3 < 2) "true"
x <- if (2 > 3) "bar" else "baz"
x_vec <- 10
ifelse(x_vec > 3, "Lon hon 3", "Nho hon hoac bang 3")
## [1] "Lon hon 3"
x_loop <- 1:5
total <- 0
for (element in x_loop) {
total <- total + element
}
print(total)
## [1] 15
NA + 5 # Kết quả là NA
## [1] NA
is.na(NA) # Kiểm tra NA -> TRUE
## [1] TRUE
is.na(4) # -> FALSE
## [1] FALSE
sum(c(1, NA, 2), na.rm = TRUE) # Tính tổng bỏ qua NA
## [1] 3
df <- data.frame(x = 1:5, y = 6:10, a = 11:15, b = 16:20)
print(df)
## x y a b
## 1 1 6 11 16
## 2 2 7 12 17
## 3 3 8 13 18
## 4 4 9 14 19
## 5 5 10 15 20
df[1, 1] # Lấy dòng 1, cột 1
## [1] 1
df[, "a"] # Lấy cột "a"
## [1] 11 12 13 14 15
df$b # Lấy cột "b" dùng dấu $
## [1] 16 17 18 19 20
library(magrittr)
x <- 10
f <- function(k) k+2
g <- function(k) k*3
x %>% f %>% g # (Chạy thử nếu muốn)
## [1] 36
d <- data.frame(x = rnorm(10), y = rnorm(10))
d %>% lm(y ~ x, data = .) # Dấu chấm (.) đại diện cho dữ liệu d
##
## Call:
## lm(formula = y ~ x, data = .)
##
## Coefficients:
## (Intercept) x
## 0.3355 0.2538
gender <- factor(c("male", "female", "female", "male"))
print(gender)
## [1] male female female male
## Levels: female male
levels(gender) # Xem nhóm
## [1] "female" "male"
nlevels(gender) # Đếm số nhóm
## [1] 2
my_list <- list(
name = "Dat",
age = 25,
scores = c(8, 9, 10),
passed = TRUE
)
my_list$name # Lấy giá trị theo tên
## [1] "Dat"
my_list[[3]] # Lấy dữ liệu thực của phần tử thứ 3
## [1] 8 9 10
my_list[3] # Lấy list con
## $scores
## [1] 8 9 10
M <- matrix(1:6, nrow = 2, ncol = 3)
print(M)
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
M[1, 2] # Dòng 1, cột 2
## [1] 3
M[2, ] # Toàn bộ dòng 2
## [1] 2 4 6
M[, 1] # Toàn bộ cột 1
## [1] 1 2
M_transpose <- t(M) # Chuyển vị
prod <- M %*% M_transpose
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
set.seed(123) # để tái lập kết quả
mean_positive <- rnorm(1000) %>%
replace(. < 0, NA) %>%
mean(na.rm = TRUE)
mean_positive
## [1] 0.7936708
library(dplyr)
df_rmse <- data.frame(
t = c(3, 5, 2.5, 7),
y = c(2.5, 5, 4, 8)
)
rmse <- df_rmse %>%
mutate(sq_error = (t - y)^2) %>%
summarise(mse = mean(sq_error)) %>%
mutate(rmse = sqrt(mse)) %>%
pull(rmse)
rmse
## [1] 0.9354143