2 + 3
3 - 6
18 * 2
## [1] 5
## [1] -3
## [1] 36
Devide integers and you will get a floating-point number
9 / 4
## [1] 2.25
And use “%/%” to get integer result
9 %/% 4
## [1] 2
“^” and “**” are used as exponentiation operators
2 ^ 3
2 ** 3
## [1] 8
## [1] 8
We use arrow operator to assign a value to a variable, for example:
x <- 19
It works in reverse direction
19 -> x
Put the assignment in parentheses to print it
(x <- "HDPE là ngon luôn")
## [1] "HDPE là ngon luôn"
#length() is used to give length of a vector
length("HDPE là ngon luôn")
## [1] 1
#if we want to know the length of a string, we must use nchar()
nchar("muối bỏ biển")
## [1] 12
R has several differences compared to other languages that index starts from 0 to n-1, index in R goes from 1 to n instead Using : operator or c() to extract a subvector:
#
HDPE <- 1:6
HDPE[3:6]
## [1] 3 4 5 6
HDPE[c(1, 4, 6)]
## [1] 1 4 6
Combine with expressions to pick values that match with expressions’ result
HDPE <- 1:6
HDPE[HDPE %% 2 == 1]
## [1] 1 3 5
Name the vector indices and use those to index the vector
HDPE <- c("muối" = 1, "bỏ" = 2, "biển" = 3)
HDPE
## muối bỏ biển
## 1 2 3
Or do it by using names():
names(HDPE) <- c("là", "ngon", "luôn")
HDPE
## là ngon luôn
## 1 2 3
phai_chiu <- 1:5
phai_chiu ** 2
## [1] 1 4 9 16 25
Use operators with vectors that have the same length or not
phai_chiu <- 6:10
lam_duoc_gi_pha_day <- 1:5
lo_roi_cac_chau_oi <- 1:3
phai_chiu - lam_duoc_gi_pha_day
## [1] 5 5 5 5 5
phai_chiu - lo_roi_cac_chau_oi
## Warning in phai_chiu - lo_roi_cac_chau_oi: longer object length is not a
## multiple of shorter object length
## [1] 5 5 5 8 8
sum_of_xy <- function(x, y){
return (x + y);
}
sum_of_xy(2, 4)
## [1] 6
HDPE <- 1:5
if (sum(HDPE) > 10){
print("Là ngon luôn");
}else{
print("Như muối bỏ biển");
}
## [1] "Là ngon luôn"
for and while loop
x = 1:5
for (x_temp in x){
print(x_temp * 2)
}
## [1] 2
## [1] 4
## [1] 6
## [1] 8
## [1] 10
index = 1
while (index <= length(x)){
x[index] = x[index] - 10
index = index + 1
}
x
## [1] -9 -8 -7 -6 -5
To categorize data
HDPE <- factor(c("bỏ", "muối", "bỏ", "biển", "muối", "biển"), levels = c("muối", "bỏ", "biển"))
ordered(HDPE)
## [1] bỏ muối bỏ biển muối biển
## Levels: muối < bỏ < biển
HDPE <- data.frame(a = 1:4 ,b = LETTERS[1:4])
HDPE[1, ]
## a b
## 1 1 A
HDPE[, "b"]
## [1] "A" "B" "C" "D"
#same result
HDPE$b
## [1] "A" "B" "C" "D"
HDPE <- c("muối", NA, "bỏ", "biển")
for (x in HDPE){
print(is.na(x))
}
## [1] FALSE
## [1] TRUE
## [1] FALSE
## [1] FALSE
library("magrittr")
x <- 1:4
avg <- function(len){
return (sum(x) / len)
}
length(x) %>% avg(.)
## [1] 2.5
avg_age <- function(number, age){
return (sum(age) / number)
}
name <- c("Nguyên", "Hải", "Độ")
age <- c(20, 21, 22)
d <- data.frame(name, age) %>% {
print(.[.$age > 20,])
}
## name age
## 2 Hải 21
## 3 Độ 22
x = rnorm(20)
x
## [1] 1.09841585 -2.08241150 0.78874866 -0.35345658 -0.48317722 1.21483735
## [7] 0.42128068 2.08602055 -1.69042191 -0.60525308 -0.76426596 1.32520518
## [13] -0.99400214 0.25289519 0.38694027 0.34638748 1.68250331 0.03534077
## [19] -1.04543033 -0.69704260
x[x < 0] <- NA
x
## [1] 1.09841585 NA 0.78874866 NA NA 1.21483735
## [7] 0.42128068 2.08602055 NA NA NA 1.32520518
## [13] NA 0.25289519 0.38694027 0.34638748 1.68250331 0.03534077
## [19] NA NA
mean(x, na.rm = TRUE)
## [1] 0.8762341