#1. Cài thư mục làm việc (Rscript)

setwd("D:/OneDrive/Studying/R-coding/RScript_Basic")
paste('one','three','2',sep=",",collapse = NULL)
## [1] "one,three,2"

‘x’ not “x”


```r
cat('one', 'three', '3',sep="-", collapse=NULL )
## one-three-3
nchar('abc')
## [1] 3
nchar('abcfeggf')
## [1] 8
grep("[a-z]",letters)
##  [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
grep(pattern='abc', x='d')
## integer(0)
grepl(pattern = 'aabc', x=
'c')
## [1] FALSE
grepl(pattern = 'Nam', x = 'Hoàng Văn Nam')
## [1] TRUE
grepl(pattern = 'Nam', x = 'Nguyễn Thị Hiền')
## [1] FALSE
1.5>3
## [1] FALSE
1.5<=3
## [1] TRUE
1.5==3
## [1] FALSE
1.5|3
## [1] TRUE
1.5||3
## [1] TRUE
age <- 99
age
## [1] 99
class(age)
## [1] "numeric"
age1 <- 99L
class(age1)
## [1] "integer"

##Chuyển dữ liệu về ngày tháng năm chuẩn

date <- as.Date('22/09/2022', format='%d/%m/%Y')
date
## [1] "2022-09-22"
date2 <- as.Date(3, origin=date)
date2
## [1] "2022-09-25"
class(date2)
## [1] "Date"
date3 <- as.numeric(date)
class(date3)
## [1] "numeric"

date.n <- as.Daten, origin = date)

Tạo nhanh một vector

vec <- c(1,2,4,5)
print(vec)
## [1] 1 2 4 5
vec1 <- 5:13
print(vec1)
## [1]  5  6  7  8  9 10 11 12 13
vec2 <- seq(5,50,by=10)
print(vec2)
## [1]  5 15 25 35 45
vec2[3]
## [1] 25
vec2[5]
## [1] 45
vec2[-1]
## [1] 15 25 35 45
vec2[c(3,5)]
## [1] 25 45
##Truy cập phần từ thứ m, n x[c(m,n)]
vec>1
## [1] FALSE  TRUE  TRUE  TRUE
vec>vec1
## Warning in vec > vec1: longer object length is not a multiple of shorter object
## length
## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
dt <- c(20,30,35)
dt
## [1] 20 30 35
names(dt) <- c("phong ngu", "phong khach", "phong bep")
print(dt)
##   phong ngu phong khach   phong bep 
##          20          30          35
vec1[2] <- 11
print(vec1)
## [1]  5 11  7  8  9 10 11 12 13
vec1 > 11
## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE
vec1[vec1>11]
## [1] 12 13
summary(vec1)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   5.000   8.000  10.000   9.556  11.000  13.000

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.