1+2
## [1] 3
3+3
## [1] 6
4/3
## [1] 1.333333
7 %/% 3
## [1] 2
7 %% 3
## [1] 1
#them L de chuyen sang kieu int
class(4L)
## [1] "integer"
class(3**6)
## [1] "numeric"
3^6
## [1] 729
3**6
## [1] 729
"hello world"
## [1] "hello world"
y<-"hola"
y
## [1] "hola"
(y <- "visible")
## [1] "visible"
x=1:36
x
## [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
length(x)
## [1] 36
length("hola")
## [1] 1
length(c("du ma","sida")) #them c o trc de noi 2 vecto
## [1] 2
nchar("1234 holisit") # do do dai chuoi
## [1] 12
(v<-1:18)
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
v[12]
## [1] 12
n<-c("hello","siba","a","b","C")
n[1:3] #hien thi ptu o vitri 1>3
## [1] "hello" "siba" "a"
n[c(2,4)] #hien thi ptu o vitri 2,4
## [1] "siba" "b"
v%%2==0
## [1] FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE
## [13] FALSE TRUE FALSE TRUE FALSE TRUE
v[v%%2==0] #hien gia tri cac ptu chia cho 2
## [1] 2 4 6 8 10 12 14 16 18
v[v%%3==0]
## [1] 3 6 9 12 15 18
p <- c("A" = 1, "B" = 2, "C" = 3) #gan ten
p
## A B C
## 1 2 3
names(p)<-c("ten 1","ten 2","ten 3") #van la gan ten
p
## ten 1 ten 2 ten 3
## 1 2 3
x<-1:4
y<-3:6
x^2-y
## [1] -2 0 4 10
ham_mu3_tong<-function(m,n){
mu_3<-m*3
mu_3+n
}
ham_mu3_tong(1:3,c(7,3,1)) #truyen tham so
## [1] 10 9 10
chuoi<-1:36
mean(chuoi) #ham tinh trung binh cong
## [1] 18.5
t<-4
if(t<2) ("sai") else ("wowowowowo")
## [1] "wowowowowo"
t<-4
p=c(1,4,7)
ifelse(t>p,("meo"),("gau")) # ifelse(test, yes, no)
## [1] "meo" "gau" "gau"
ham <- function(x) {
if (x %% 2 == 0) {
x ** 2
} else {
x
}
}
ham <- Vectorize(ham)
ham(1:5)
## [1] 1 4 3 16 5
ham_1<-function(x)
ifelse(x %%2 == 0,x^2,x)
ham_1(1:5)
## [1] 1 4 3 16 5
rauma<-c("bo","re","hdpe","thi","ngon","luon")
for(i in rauma){
if(nchar(i) >=3){
print(i)
}
}
## [1] "hdpe"
## [1] "thi"
## [1] "ngon"
## [1] "luon"
wtf<-c(1,4,6)
tong_1<-0
for(i in wtf) tong_1<-tong_1+i # duyet gia tri
tong_1
## [1] 11
tong_2<-0
for(i in seq_along(wtf)) tong_2<-tong_2+i # "seq_along" duyet chi so
tong_2
## [1] 6
x <- c("Nam", "Nữ", "Nam", "Nam","lgbt","ok","hi")
f <- factor(x)
f
## [1] Nam Nữ Nam Nam lgbt ok hi
## Levels: hi lgbt Nam Nữ ok
factor(
f,
levels = c("Nữ", "Nam", "lgbt", "ok")
)
## [1] Nam Nữ Nam Nam lgbt ok <NA>
## Levels: Nữ Nam lgbt ok
df <- data.frame(x = 2:5, y = c("hi","hola","helo","bye"), z = letters[1:4])
df
## x y z
## 1 2 hi a
## 2 3 hola b
## 3 4 helo c
## 4 5 bye d
# cot 1: x tu 2->5
# cot 2: "hi","hola","helo","bye"
# cot 3: a b c d
df[3:1] # df[a:b] : lay cot tu a -> b
## z y x
## 1 a hi 2
## 2 b hola 3
## 3 c helo 4
## 4 d bye 5
df[2,] # df[a,] : lay hang thu a
## x y z
## 2 3 hola b
df[,"z"] #df[,"a"] : lay hang co ten "a"
## [1] "a" "b" "c" "d"
df$z # tuong tu df[,"a"]
## [1] "a" "b" "c" "d"
df <- data.frame(x = 2:5, y = c("hi","hola","helo","bye"), z = letters[1:4])
df
## x y z
## 1 2 hi a
## 2 3 hola b
## 3 4 helo c
## 4 5 bye d
df2 <- data.frame(x=1:2,y=c("moe","ok"),z=letters[5:6])
rbind(df,df2) # them vao data frame cac hang moi(phai trung voi ten cot da co)
## x y z
## 1 2 hi a
## 2 3 hola b
## 3 4 helo c
## 4 5 bye d
## 5 1 moe e
## 6 2 ok f
df3 <- data.frame(p=1:2,q=c("moe","ok"),r=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 z p q r
## 1 2 hi a 1 moe e
## 2 3 hola b 2 ok f
## 3 4 helo c 1 moe e
## 4 5 bye d 2 ok f
v=c(1,2,5,9,NA)
l=c(1,5,3)
is.na(v) # kiem tra trong v co NA hay ko
## [1] FALSE FALSE FALSE FALSE TRUE
is.na(l)
## [1] FALSE FALSE FALSE