---
title: "DaPS_SummaryGuide"
format: html
editor: visual
---
# Data Types & Operations
## Numeric (Double Precision)
```{r}
a <- 10.25 # assign numeric value (double)
b <- -25 # negative number
typeof(a) # returns "double" (storage type)
class(a) # returns "numeric" (object class)
is.numeric(a) # TRUE if numeric (int or double)
is.double(a) # TRUE if double precision
```
```{r}
a <- "12.25" # now a is a character string
is.numeric(a) # FALSE
typeof(a) # "character"
b <- as.numeric(a) # convert character to numeric
```
***Arithmetic operators:***
```{r}
a <- 50
b <- 20
a + b # addition → 70
a - b # subtraction → 30
a * b # multiplication → 1000
a / b # division → 2.5
a %/% b # integer division → 2
a %% b # modulus (remainder) → 10
```
*integer division \* divisor + remainder = dividend*
```{r}
c <- a %/% b
d <- a %% b
c * b + d # should equal original a (50)
```
*even/odd check:*
```{r}
31 %% 2 # 1 → odd
30 %% 2 # 0 → even
```
***Mathematical Functions:***
```{r}
abs(-50) # absolute value → 50
log(a) # natural log (ln)
,log10(a) # log base 10
log(a, base=10) # equivalent to log10
sqrt(a) # square root
a^3 # a cubed
a^(1/3) # cube root
exp(a) # e^a
log(exp(a)) # back to original a
```
## Statistical Functions (Normal Distribution)
```{r}
v_norm <- rnorm(n = 10000, mean = 0, sd = 1) # generate 10,000 values from normal
distribution
hist(v_norm, probability = TRUE, col = "lightgrey") # histogram
lines(density(v_norm), lwd = 3, col = "darkgrey") # add density line
abline(v = 1.75, lwd = 3, col = "orange") # vertical reference line
```
```{r}
pnorm(1.75, 0, 1) # P(X ≤ 1.75)
pnorm(1.75, 0, 1, lower.tail = FALSE) # P(X > 1.75)
dnorm(1.75, 0, 1) # density (height) at 1.75
qnorm(0.95, 0, 1) # 95th percentile (quantile)
```
***Inverse Functions***
```{r}
qnorm(pnorm(1.75, lower.tail = FALSE), lower.tail = FALSE)
pnorm(qnorm(0.95), 0, 1)
```
## Rounding & Truncating
```{r}
round(0.5) # 0 → /!\ rounds to nearest even
round(1.5) # 2
round(-0.5) # 0
round(-1.5) # -2
```
```{r}
round(12.34567, digits = 4) # → 12.3457
round(12.34567, digits = 3) # → 12.346
round(12.34567, digits = -1) # → 10 (round to tens)
```
```{r}
floor(12.34567) # → 12 (round down)
ceiling(12.34567) # → 13 (round up)
trunc(12.34567) # → 12 (drop decimals)
```
```{r}
signif(123456789.12, digits = 3) # → 1.23e+08
```
NOTE: round() → decimal digits, signif() → total significant figures.
,***NaN & Inf:***
```{r}
typeof(NaN) # "double"
a <- 0/0 # NaN: undefined
log(-10) # NaN
typeof(Inf) # "double"
a <- 50/0 # Inf: positive infinity
is.infinite(a) # TRUE
is.finite(a) # FALSE
a * 2 # still Inf
```
## Integer
```{r}
a <- 10L # append L to define an integer
typeof(a) # "integer"
is.integer(a) # TRUE
```
```{r}
a <- "10"
b <- as.integer(a) # convert character to integer
```
Math returns double:
```{r}
a <- 50L
b <- 2
c <- a / b
typeof(c) # "double"
```
## Logical / Boolean
```{r}
bool_1 <- TRUE
bool_2 <- FALSE
is.logical(bool_1) # TRUE
typeof(bool_1) # "logical"
```
***Boolean as numbers:***
```{r}
TRUE + 1 # 2
FALSE + 1 # 1
TRUE + TRUE - FALSE # 2
```
***Comparisons:***
```{r}
a == b # equal to
a != b # not equal to
a > b; a >= b # greater than (or equal)
, a < b; a <= b # less than (or equal)
!(a == b) # NOT
(a > b) & (a < c) # AND
(a < b) | (a > c) # OR
```
***Floating point check:***
```{r}
a <- 1/3
b <- 0.333333333333333
abs(a - b) < 1e-12 # safer than a == b
```
## Characters / Strings
```{r}
char1 <- "Hello"
typeof(char1) # "character"
is.character(char1) # TRUE
length(char1) # 1 (always 1 for strings)
```
### String operations
***case:***
```{r}
tolower("ABC") # → "abc"
toupper("abc") # → "ABC"
stringr::str_to_title("hello world") # → "Hello World"
```
***combine:***
```{r}
paste("a", "b") # → "a b"
paste("a", "b", sep = "_") # → "a_b"
paste0("a", "b") # → "ab"
glue::glue("Hi {name}") # string interpolation
```
***split:***
```{r}
char1 <- "Bachelor of Business Administration"
strsplit(x = char1, split = " ") # → "Bachelor" "of" "Business" "Administration"
```
***pipe-friendly:***
```{r}
char_a |> paste0(char_b)
stringr::str_c(char_a, char_b, sep = ": ")
stringr::str_glue(char_a, ": ", surname)
```
### Pattern search
title: "DaPS_SummaryGuide"
format: html
editor: visual
---
# Data Types & Operations
## Numeric (Double Precision)
```{r}
a <- 10.25 # assign numeric value (double)
b <- -25 # negative number
typeof(a) # returns "double" (storage type)
class(a) # returns "numeric" (object class)
is.numeric(a) # TRUE if numeric (int or double)
is.double(a) # TRUE if double precision
```
```{r}
a <- "12.25" # now a is a character string
is.numeric(a) # FALSE
typeof(a) # "character"
b <- as.numeric(a) # convert character to numeric
```
***Arithmetic operators:***
```{r}
a <- 50
b <- 20
a + b # addition → 70
a - b # subtraction → 30
a * b # multiplication → 1000
a / b # division → 2.5
a %/% b # integer division → 2
a %% b # modulus (remainder) → 10
```
*integer division \* divisor + remainder = dividend*
```{r}
c <- a %/% b
d <- a %% b
c * b + d # should equal original a (50)
```
*even/odd check:*
```{r}
31 %% 2 # 1 → odd
30 %% 2 # 0 → even
```
***Mathematical Functions:***
```{r}
abs(-50) # absolute value → 50
log(a) # natural log (ln)
,log10(a) # log base 10
log(a, base=10) # equivalent to log10
sqrt(a) # square root
a^3 # a cubed
a^(1/3) # cube root
exp(a) # e^a
log(exp(a)) # back to original a
```
## Statistical Functions (Normal Distribution)
```{r}
v_norm <- rnorm(n = 10000, mean = 0, sd = 1) # generate 10,000 values from normal
distribution
hist(v_norm, probability = TRUE, col = "lightgrey") # histogram
lines(density(v_norm), lwd = 3, col = "darkgrey") # add density line
abline(v = 1.75, lwd = 3, col = "orange") # vertical reference line
```
```{r}
pnorm(1.75, 0, 1) # P(X ≤ 1.75)
pnorm(1.75, 0, 1, lower.tail = FALSE) # P(X > 1.75)
dnorm(1.75, 0, 1) # density (height) at 1.75
qnorm(0.95, 0, 1) # 95th percentile (quantile)
```
***Inverse Functions***
```{r}
qnorm(pnorm(1.75, lower.tail = FALSE), lower.tail = FALSE)
pnorm(qnorm(0.95), 0, 1)
```
## Rounding & Truncating
```{r}
round(0.5) # 0 → /!\ rounds to nearest even
round(1.5) # 2
round(-0.5) # 0
round(-1.5) # -2
```
```{r}
round(12.34567, digits = 4) # → 12.3457
round(12.34567, digits = 3) # → 12.346
round(12.34567, digits = -1) # → 10 (round to tens)
```
```{r}
floor(12.34567) # → 12 (round down)
ceiling(12.34567) # → 13 (round up)
trunc(12.34567) # → 12 (drop decimals)
```
```{r}
signif(123456789.12, digits = 3) # → 1.23e+08
```
NOTE: round() → decimal digits, signif() → total significant figures.
,***NaN & Inf:***
```{r}
typeof(NaN) # "double"
a <- 0/0 # NaN: undefined
log(-10) # NaN
typeof(Inf) # "double"
a <- 50/0 # Inf: positive infinity
is.infinite(a) # TRUE
is.finite(a) # FALSE
a * 2 # still Inf
```
## Integer
```{r}
a <- 10L # append L to define an integer
typeof(a) # "integer"
is.integer(a) # TRUE
```
```{r}
a <- "10"
b <- as.integer(a) # convert character to integer
```
Math returns double:
```{r}
a <- 50L
b <- 2
c <- a / b
typeof(c) # "double"
```
## Logical / Boolean
```{r}
bool_1 <- TRUE
bool_2 <- FALSE
is.logical(bool_1) # TRUE
typeof(bool_1) # "logical"
```
***Boolean as numbers:***
```{r}
TRUE + 1 # 2
FALSE + 1 # 1
TRUE + TRUE - FALSE # 2
```
***Comparisons:***
```{r}
a == b # equal to
a != b # not equal to
a > b; a >= b # greater than (or equal)
, a < b; a <= b # less than (or equal)
!(a == b) # NOT
(a > b) & (a < c) # AND
(a < b) | (a > c) # OR
```
***Floating point check:***
```{r}
a <- 1/3
b <- 0.333333333333333
abs(a - b) < 1e-12 # safer than a == b
```
## Characters / Strings
```{r}
char1 <- "Hello"
typeof(char1) # "character"
is.character(char1) # TRUE
length(char1) # 1 (always 1 for strings)
```
### String operations
***case:***
```{r}
tolower("ABC") # → "abc"
toupper("abc") # → "ABC"
stringr::str_to_title("hello world") # → "Hello World"
```
***combine:***
```{r}
paste("a", "b") # → "a b"
paste("a", "b", sep = "_") # → "a_b"
paste0("a", "b") # → "ab"
glue::glue("Hi {name}") # string interpolation
```
***split:***
```{r}
char1 <- "Bachelor of Business Administration"
strsplit(x = char1, split = " ") # → "Bachelor" "of" "Business" "Administration"
```
***pipe-friendly:***
```{r}
char_a |> paste0(char_b)
stringr::str_c(char_a, char_b, sep = ": ")
stringr::str_glue(char_a, ": ", surname)
```
### Pattern search