x <- 2
if (x<0) {print("am")
} else {print("duong")
}
## [1] "duong"
if (condition1) else { print(“a111”)} else if (condition2) {print(“a2222”)} else {print(“a333”) }
x <- 4
if (x<0) {print("am")
} else if (x==0) {print("bang 0")
} else {print("so duong")
}
## [1] "so duong"
# Extend/adapt the while loop
speed <- 64
while (speed > 30) {
print(paste("Your speed is", speed))
if (speed > 48) {
print("Slow down big time!")
speed <- speed - 11
} else {
print("Slow down!")
speed <- speed - 6
}
}
## [1] "Your speed is 64"
## [1] "Slow down big time!"
## [1] "Your speed is 53"
## [1] "Slow down big time!"
## [1] "Your speed is 42"
## [1] "Slow down!"
## [1] "Your speed is 36"
## [1] "Slow down!"
li <- c("ab",
"b",
"abc")
for (ch in li) {
if (nchar(ch)>2) {
next # next is skip ch co so ki tu nhieu hon 2
}
print(ch)
}
## [1] "ab"
## [1] "b"
for (i in 1:length(li)) {
print(li[i])
}
## [1] "ab"
## [1] "b"
## [1] "abc"
for (i in 1:length(li)) {
print(paste(li[i],"is position",
i, "in vector li"))
}
## [1] "ab is position 1 in vector li"
## [1] "b is position 2 in vector li"
## [1] "abc is position 3 in vector li"
##Function
l <- c(2,4,NA)
sd(l,na.rm = T)# dung n.rm=T de remove NA. unless the result will display: NA
## [1] 1.414214