R-Markdown is an useful tool for creating ”HTML” files that document R code and outputs.
An effective way to utilize R-Markdown is through the RStudio environment.
Simply create a new R Markdown file and install the necessary packages.
To enhance the visual appeal of your R-Markdown documents, consider implementing the following steps:
install.packages("remotes")
remotes::install_github("rlesur/klippy")
---
title: "Analysis of Spatial and Temporal Data"
author: "by Nguyen-Xuan Thanh"
date: "2025-01"
output:
html_document:
code_download: TRUE
code_folding: show
number_sections: TRUE
theme: flatly
toc: TRUE
toc_float: TRUE
dev: "svg"
---
For more details on using R Markdown see following documents:
original documents: http://rmarkdown.rstudio.com
or here (highly recommended): https://bookdown.org/yihui/rmarkdown/
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 also consider to tick in Knit on Save button.
If you are not familiar with R-Markdown yet, you can enable Visual Editor in RStudio, which feels similar to traditional editors like Word.
With Visual Editor, you can make some works become more simple, e.g. inserting images or screenshot as below:
1 + 1
## [1] 2
1:10
## [1] 1 2 3 4 5 6 7 8 9 10
rc <- 5 # radius of a circle
inline R code: For a circle with the radius 5, its area is 78.5398163.
You can also embed plot, for example:
plot(1:10, 1:10)
This is the first plot with R-Markdown
echo = FALSE
parameter can be added to
the code chunk to prevent printing of the R code that generated the
plot.# Just simply typing in
"Hello World!"
## [1] "Hello World!"
# Using "print" command
print("Hello World!")
## [1] "Hello World!"
# Using "paste" command
print(paste("Hello", "World!", "1"))
## [1] "Hello World! 1"
# Combination of "print" & "paste"
print(paste("Hello", "World!", "1", sep="___"))
## [1] "Hello___World!___1"
# "paste0" command is also popular
paste0("Hello ", "World! ", "2")
## [1] "Hello World! 2"
# To known more details of a built-in function: use "help"
help("paste0")
# or
??paste0
# Print a number
1
## [1] 1
# Print a sequence of numbers. Interval always=1
1:100
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
## [19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
## [37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
## [55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
## [73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
## [91] 91 92 93 94 95 96 97 98 99 100
# Using "seq". Use help(seq) or ??seq to read more details
seq(10)
## [1] 1 2 3 4 5 6 7 8 9 10
# Some others way to create/print a sequence
100:130
## [1] 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
## [20] 119 120 121 122 123 124 125 126 127 128 129 130
# or with different interval
seq(100, 130, 2)
## [1] 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130
# A Vectors can be defined as
c(1, 3, 5, 10, 999, NA, "something", TRUE, FALSE)
## [1] "1" "3" "5" "10" "999" NA
## [7] "something" "TRUE" "FALSE"
3 + 7 # Add
## [1] 10
3 - 7 # Sub
## [1] -4
3 * 7 # Mul
## [1] 21
3 / 7 # Div
## [1] 0.4285714
3 ^ 7 # Pow
## [1] 2187
3 ** 7 # Pow (same as above)
## [1] 2187
Be aware for the order of operators
2**3**2
## [1] 512
(2**3)**2
## [1] 64
2**(3**2)
## [1] 512
Multiple line calculation
5 -
+ 1 +
+ 3
## [1] 7
obj.1 <- 1 ; obj.1
## [1] 1
obj.2 <- 1:10 ; obj.2
## [1] 1 2 3 4 5 6 7 8 9 10
obj.3 = 3 ; obj.3
## [1] 3
Obj.1 = 10 ; Obj.1
## [1] 10
Variables in r are case-sensitive
paste0("obj.1 = ", obj.1)
## [1] "obj.1 = 1"
paste0("Obj.1 = ", Obj.1)
## [1] "Obj.1 = 10"
Variable can be a result of function(s)
obj.4 = seq(from=1, to=100, by=3) ; obj.4
## [1] 1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55
## [20] 58 61 64 67 70 73 76 79 82 85 88 91 94 97 100
obj.5 = rep(seq(100, 0, -10), 3) ; obj.5
## [1] 100 90 80 70 60 50 40 30 20 10 0 100 90 80 70 60 50 40 30
## [20] 20 10 0 100 90 80 70 60 50 40 30 20 10 0
pi
## [1] 3.141593
NaN
## [1] NaN
Inf
## [1] Inf
-Inf
## [1] -Inf
exp(1) # i.e., e
## [1] 2.718282
.Machine
## $double.eps
## [1] 2.220446e-16
##
## $double.neg.eps
## [1] 1.110223e-16
##
## $double.xmin
## [1] 2.225074e-308
##
## $double.xmax
## [1] 1.797693e+308
##
## $double.base
## [1] 2
##
## $double.digits
## [1] 53
##
## $double.rounding
## [1] 5
##
## $double.guard
## [1] 0
##
## $double.ulp.digits
## [1] -52
##
## $double.neg.ulp.digits
## [1] -53
##
## $double.exponent
## [1] 11
##
## $double.min.exp
## [1] -1022
##
## $double.max.exp
## [1] 1024
##
## $integer.max
## [1] 2147483647
##
## $sizeof.long
## [1] 8
##
## $sizeof.longlong
## [1] 8
##
## $sizeof.longdouble
## [1] 8
##
## $sizeof.pointer
## [1] 8
##
## $sizeof.time_t
## [1] 8
letters
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
## [20] "t" "u" "v" "w" "x" "y" "z"
LETTERS
## [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
## [20] "T" "U" "V" "W" "X" "Y" "Z"
month.abb
## [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
month.name
## [1] "January" "February" "March" "April" "May" "June"
## [7] "July" "August" "September" "October" "November" "December"
factorial(5)
## [1] 120
sqrt(9)
## [1] 3
sqrt(1:10)
## [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427
## [9] 3.000000 3.162278
obj.4
## [1] 1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55
## [20] 58 61 64 67 70 73 76 79 82 85 88 91 94 97 100
summary(obj.4)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.00 25.75 50.50 50.50 75.25 100.00
min(obj.4)
## [1] 1
max(obj.4)
## [1] 100
quantile(obj.4, 0.25)
## 25%
## 25.75
quantile(obj.4, 0.50)
## 50%
## 50.5
quantile(obj.4, 0.75)
## 75%
## 75.25
IQR(obj.4)
## [1] 49.5
f.1 = function(x, y, z) {
return(2*x^2 + 5*y - z)
}
f.1(5, 6, 1)
## [1] 79
EOF