Bieu thuc don gian

1 + 2
## [1] 3
4 / 2
## [1] 2
(2 + 2) * 3
## [1] 12

chia lay phan nguyen

7 %/% 3
## [1] 2

chia lay du

7 / 3
## [1] 2.333333

them L de chuyen sang kieu integer

class(4L)
## [1] "integer"

mot so toan tu luy thua

class(3**6)
## [1] "numeric"
3^6
## [1] 729
3**6
## [1] 729

chuoi

"hello world"
## [1] "hello world"

gan

x <- 2
x
## [1] 2
x <- "invisible"
x
## [1] "invisible"

bieu thuc

x <- 2
2 * x
## [1] 4

vecto

1:50
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

do dai chuoi

length("qax")
## [1] 1
length(c("foo", "barz")) #them c o trc de noi 2 vecto
## [1] 2

do dai chuoi thuc te

nchar("qax")
## [1] 3
nchar(c("foo", "barz")) 
## [1] 3 4

vecto lap chi muc

(v<-1:5)
## [1] 1 2 3 4 5
v[3] #hien thi so o vitri thu 3
## [1] 3
v[1:3] #hien thi chuoi 1 den 3
## [1] 1 2 3
v[c(1,3,5)] #hien thi o vi tri nhat dinh
## [1] 1 3 5
v[c(TRUE, FALSE, TRUE, FALSE, TRUE)] #hien thi cac so o vitri TRUE
## [1] 1 3 5
v %% 2 == 0 
## [1] FALSE  TRUE FALSE  TRUE FALSE
v[v %% 2 == 0]
## [1] 2 4
v[-(1:3)] #hien thi cac gia tri con lai
## [1] 4 5

dat ten cho cac vecto

v <- c("A" = 1, "B" = 2, "C" = 3)
v
## A B C 
## 1 2 3
names(v) <- c("x", "y", "z")
v
## x y z 
## 1 2 3

bieu thuc vecto hoa

x<-1:4
y<-3:6
x^2-y
## [1] -2  0  4 10

Funtions

square <- function(x) x**2
square(1:4)
## [1]  1  4  9 16
square_and_subtract <- function(x, y) {
squared <- x ** 2
squared - y
}
square_and_subtract(1:5, rev(1:5)) 
## [1] -4  0  6 14 24

bieu thuc va ham vecto hoa

sum(1:4) 
## [1] 10
average <- function(x) {
n <- length(x)
sum(x) / n
}
average(1:5) 
## [1] 3

cac cau truc dieu khien(if/else)

t<-4
if(t<2) ("sai") else ("Dung")
## [1] "Dung"
n=c(2,4,9)
ifelse(t>n,("dung"),("sai")) #ifelse(test, yes, no)
## [1] "dung" "sai"  "sai"

cau truc for

ds<-c("mot","hai","ba","bon","nam","sau")
for(i in ds){
  if(nchar(i) >=3){
    print(i)
  }
}
## [1] "mot"
## [1] "hai"
## [1] "bon"
## [1] "nam"
## [1] "sau"

for(i in x) : duyet theo gia tri

day<-c(1,4,6)
tong_1<-0
for(i in day) tong_1<-tong_1+i # duyet gia tri
tong_1
## [1] 11

for(i in seq_along(x)) : duyet theo vi tri

tong_2<-0
for(i in seq_along(day)) tong_2<-tong_2+i # "seq_along" duyet chi so
tong_2
## [1] 6

Factor

ff <- factor(c("small", "small", "medium",
"large", "small", "large"),
levels = c("small", "medium", "large"))
ff
## [1] small  small  medium large  small  large 
## Levels: small medium large

data frame

df <- data.frame(x = 1:4, y = letters[1:4])
df
##   x y
## 1 1 a
## 2 2 b
## 3 3 c
## 4 4 d
# cot 1: x tu 1->4
# cot 3: a b c d
df[2:1] # df[a:b] : lay cot tu a -> b
##   y x
## 1 a 1
## 2 b 2
## 3 c 3
## 4 d 4
df[1,] # df[a,] : lay hang thu a
##   x y
## 1 1 a
df[,"x"] #df[,"a"] : lay hang co ten "a"
## [1] 1 2 3 4
df$y # tuong tu df[,"a"]
## [1] "a" "b" "c" "d"

noi data frame

df <- data.frame(x = 2:5, y = letters[1:4])
df
##   x y
## 1 2 a
## 2 3 b
## 3 4 c
## 4 5 d
df2 <- data.frame(x=1:2,y=letters[5:6])
rbind(df,df2) # them vao data frame cac hang moi(phai trung voi ten cot da co)
##   x y
## 1 2 a
## 2 3 b
## 3 4 c
## 4 5 d
## 5 1 e
## 6 2 f
df3 <- data.frame(p=1:2,q=letters[5:6])
cbind(df,df3) # them vao data frame cac cot moi(neu ko du thi du lieu se duoc lap lai)
##   x y p q
## 1 2 a 1 e
## 2 3 b 2 f
## 3 4 c 1 e
## 4 5 d 2 f