Question 1 \begin{aligned} &3^4-\frac{2^6}{4}+\sqrt{225}-\ln \left(e^5\right)+\frac{\left(\lceil 7.2 \rceil \times \lfloor 9.9 \rfloor -(29 \bmod 6)^2\right)}{5} \end{aligned}
3^4 - 2^6/4 + sqrt(225) - log(exp(5)) + (ceiling(7.2) * floor(9.9) - (29%%6)^2)/5
[1] 84.4
Question 2 \begin{aligned} &\sin (\pi / 2)+\cos (\pi / 3)-\tan (\pi / 4)+2^{1.5}-9^{1 / 2}+\frac{5^3-4^4}{3^2+2^5} \end{aligned}
sin(pi/2) + cos(pi/3) - tan(pi/4) + 2^1.5 - 9^(1/2) + (5^3-4^4)/(3^2+2^5)
[1] -2.866695
Question 3 \begin{aligned} &\sqrt{2^{10}+3^6}-\ln \left(\frac{e^8}{e^3}\right)+\frac{\ln (1000)}{\ln (10)}+e^0+\frac{\lfloor 123.99\rfloor-\lceil 45.01\rceil}{7} \end{aligned}
sqrt(2^10+3^6) - log(exp(8)/exp(3)) + (log(1000)/log(10)) + exp(0) + (floor(123.99-ceiling(45.01)))/7
[1] 51.86884
Question 4 \begin{aligned} &\frac{7^5-6^4+5^3-4^2+3}{\frac{2^8-1}{3^3-1}+(10 \bmod 6)+\sqrt{144}}+\frac{\ln \left(e^{12}\right)-\ln \left(e^7\right)}{\lceil 19.01\rceil-\lfloor 3.99\rfloor}-\left(\frac{1+2+3+\cdots+30}{1 \times 2 \times 3 \times \cdots \times 10}\right)^2 \end{aligned}
q4_1 <- (7^5-6^4+5^3-4^2+3) / ((2^8-1)/(3^3-1) + (10 %% 6) + sqrt(144))
q4_2 <- (log(exp(12)) - log(exp(7))) / (ceiling(19.01) - floor(3.99))
q4_3 <- (sum(1:30) / factorial(30))^2
q4_1 + q4_2 - q4_3
[1] 605.6563
Question 5 \begin{aligned} &\int_{-\infty}^{1.96} \frac{1}{\sqrt{2 \pi}} e^{-\frac{1}{2} x^2} d x \end{aligned}
pnorm(q = 1.96, mean = 0, sd = 1)
[1] 0.9750021
Question 6
Given \begin{aligned} &\mathbf{A}=\left[\begin{array}{lll} 2 & 1 & 3 \\ 1 & 0 & 2 \\ 4 & 1 & 1 \end{array}\right] \text { and } \mathbf{B}=\left[\begin{array}{lll} 1 & 2 & 0 \\ 3 & 1 & 4 \\ 2 & 5 & 1 \end{array}\right] \end{aligned}
Then, calculate
\mathbf{C} = \mathbf{A} \mathbf{B}
\mathbf{C}^\top \mathbf{C}
(\mathbf{C}^\top \mathbf{C})^{-1}
A <- matrix(c(2,1,4, 1,0,1, 3,2,1), ncol = 3, nrow = 3)
print(A)
[,1] [,2] [,3]
[1,] 2 1 3
[2,] 1 0 2
[3,] 4 1 1
B <- matrix(c(1,3,2, 2,1,5, 0,4,1), ncol = 3, nrow = 3)
print(B)
[,1] [,2] [,3]
[1,] 1 2 0
[2,] 3 1 4
[3,] 2 5 1
C <- A %*% B
print(C)
[,1] [,2] [,3]
[1,] 11 20 7
[2,] 5 12 2
[3,] 9 14 5
t(C) %*% C
[,1] [,2] [,3]
[1,] 227 406 132
[2,] 406 740 234
[3,] 132 234 78
solve(t(C) %*% C)
[,1] [,2] [,3]
[1,] 1.0164609 -0.26748971 -0.9176955
[2,] -0.2674897 0.09670782 0.1625514
[3,] -0.9176955 0.16255144 1.0781893
my_integer <- 42
class(my_integer)
[1] "numeric"
my_integer2 <- 42L
class(my_integer2)
[1] "integer"
my_numeric <- 42.5
class(my_numeric)
[1] "numeric"
my_character <- "some text"
class(my_character)
[1] "character"
my_character2 <- "42"
class(my_character2)
[1] "character"
my_logical <- TRUE
class(my_logical)
[1] "logical"
data <- iris
class(data)
[1] "data.frame"
names(data)
[1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
head(data, 10)
x_vec <- 1:24
print(x)
[1] 25 24 30 78 21 30 37
x_mat <- matrix(1:24, nrow=4, ncol=6)
print(x_mat)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 5 9 13 17 21
[2,] 2 6 10 14 18 22
[3,] 3 7 11 15 19 23
[4,] 4 8 12 16 20 24
x_arr <- array (1:24, c(3, 4, 2))
print(x_arr)
, , 1
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
, , 2
[,1] [,2] [,3] [,4]
[1,] 13 16 19 22
[2,] 14 17 20 23
[3,] 15 18 21 24
set.seed(123)
n <- 1000
x <- rbinom(n, size = 1, prob = 0.5)
table(x)
x
0 1
507 493
head(x, 30)
[1] 0 1 0 1 1 0 1 1 1 0 1 0 1 1 0 1 0 0 0 1 1 1 1 1 1 1 1 1 0 0
r <- rle(x)
str(r)
List of 2
$ lengths: int [1:489] 1 1 1 2 1 3 1 1 1 2 ...
$ values : int [1:489] 0 1 0 1 0 1 0 1 0 1 ...
- attr(*, "class")= chr "rle"
head(r$lengths, 10)
[1] 1 1 1 2 1 3 1 1 1 2
head(r[[1]], 10)
[1] 1 1 1 2 1 3 1 1 1 2
gender_vector <- c("Male", "Female", "Female", "Male", "Male")
class(gender_vector)
[1] "character"
class(gender_vector)
[1] "character"
factor_gender_vector <- factor(gender_vector)
factor_gender_vector
[1] Male Female Female Male Male
Levels: Female Male
class(factor_gender_vector)
[1] "factor"
TRUE == TRUE
[1] TRUE
"hello" == "goodbye"
[1] FALSE
TRUE == FALSE
[1] FALSE
3 == 2
[1] FALSE
TRUE != TRUE
[1] FALSE
"hello" != "goodbye"
[1] TRUE
TRUE != FALSE
[1] TRUE
3 != 2
[1] TRUE
3 > 5
[1] FALSE
4 <= 4
[1] TRUE
# Alphabetical Order!
"Hello" > "Goodbye"
[1] TRUE
# TRUE coerces to 1
# FALSE coerces to 0
TRUE < FALSE
[1] FALSE
linkedin <- c (16, 9, 13, 5, 2, 17, 14)
linkedin > 10
[1] TRUE FALSE TRUE FALSE FALSE TRUE TRUE
x <- 17
x > 5 & x < 15
[1] FALSE
y <- 14
y < 5 | y > 15
[1] FALSE
c(TRUE, TRUE, FALSE) & c(TRUE, FALSE, FALSE)
[1] TRUE FALSE FALSE
Convert Nilai Angka to Nilai Huruf
| Nilai Angka | Nilai Huruf |
|---|---|
| 86–100 | A |
| 76–85 | AB |
| 66–75 | B |
| 61–65 | BC |
| 56–60 | C |
| 41–55 | D |
| 0–40 | E |
convert_score <- function(x){
x <- round(x,0)
if (x >= 86 & x <= 100){
return("A")
} else if (x >= 76 & x <= 85){
return("AB")
} else if (x >= 66 & x <= 75){
return("B")
} else if (x >= 61 & x <= 65){
return("BC")
} else if (x >= 56 & x <= 60){
return("C")
} else if (x >= 41 & x <= 55){
return("D")
} else if (x >= 0 & x <= 40){
return("E")
} else{
return(NA)
}
}
x <- c(85.5)
convert_score(x)
[1] "A"
ctr <- 1
while(ctr <= 7) {
print(paste("ctr is set to" , ctr))
ctr <- ctr + 1
}
[1] "ctr is set to 1"
[1] "ctr is set to 2"
[1] "ctr is set to 3"
[1] "ctr is set to 4"
[1] "ctr is set to 5"
[1] "ctr is set to 6"
[1] "ctr is set to 7"
cities <- c("New York", "Paris",
"London", "Tokyo",
"Rio de Janeiro", "Cape Town")
for (city in cities){
print(city)
}
[1] "New York"
[1] "Paris"
[1] "London"
[1] "Tokyo"
[1] "Rio de Janeiro"
[1] "Cape Town"
cities <- c("New York", "Paris",
"London", "Tokyo",
"Rio de Janeiro", "Cape Town")
for (i in 1:length(cities)){
print(cities[i])
}
[1] "New York"
[1] "Paris"
[1] "London"
[1] "Tokyo"
[1] "Rio de Janeiro"
[1] "Cape Town"
average <- function(x){
n <- length(x)
sumX <- 0
for (i in 1:n){
sumX <- sumX + x[i]
}
return (sumX/n)
}
x <- c(25,24,30, 78, 21, 30, 37)
average(x)
[1] 35
mean(x)
[1] 35
Question
Create a function for sample variance
S^2=\frac{1}{n-1} \sum_{i=1}^n\left(x_i-\bar{x}\right)^2
sample_var <- function(x){
n <- length(x)
sumX <- 0
for (i in 1:n){
sumX <- sumX + x[i]
}
meanX <- sumX /n
varX <- 0
for (i in 1:n){
varX <- varX + (x[i]-meanX)^2
}
return(varX/(n-1))
}
sample_var(x)
[1] 386.6667
var(x)
[1] 386.6667
link_url <- "https://github.com/novrisuhermi/dataset/raw/refs/heads/main/data.csv"
data <- read.csv(link_url, header = TRUE, sep = ",")
head(data, 10)
library(readxl)
download.file(
"https://raw.githubusercontent.com/novrisuhermi/dataset/main/data.xlsx",
destfile = "data.xlsx",
mode = "wb"
)
trying URL 'https://raw.githubusercontent.com/novrisuhermi/dataset/main/data.xlsx'
Content type 'application/octet-stream' length 13255 bytes (12 KB)
==================================================
downloaded 12 KB
data2 <- read_excel("data.xlsx")
head(data2, 10)
library(summarytools)
summaryData <- descr(data)
print(summaryData)
Descriptive Statistics
data
N: 32
am carb cyl disp drat gear hp mpg qsec
----------------- -------- -------- -------- -------- -------- -------- -------- -------- --------
Mean 0.41 2.81 6.19 230.72 3.60 3.69 146.69 20.09 17.85
Std.Dev 0.50 1.62 1.79 123.94 0.53 0.74 68.56 6.03 1.79
Min 0.00 1.00 4.00 71.10 2.76 3.00 52.00 10.40 14.50
Q1 0.00 2.00 4.00 120.65 3.08 3.00 96.00 15.35 16.88
Median 0.00 2.00 6.00 196.30 3.70 4.00 123.00 19.20 17.71
Q3 1.00 4.00 8.00 334.00 3.92 4.00 180.00 22.80 18.90
Max 1.00 8.00 8.00 472.00 4.93 5.00 335.00 33.90 22.90
MAD 0.00 1.48 2.97 140.48 0.70 1.48 77.10 5.41 1.42
IQR 1.00 2.00 4.00 205.18 0.84 1.00 83.50 7.38 2.01
CV 1.23 0.57 0.29 0.54 0.15 0.20 0.47 0.30 0.10
Skewness 0.36 1.05 -0.17 0.38 0.27 0.53 0.73 0.61 0.37
SE.Skewness 0.41 0.41 0.41 0.41 0.41 0.41 0.41 0.41 0.41
Kurtosis -1.92 1.26 -1.76 -1.21 -0.71 -1.07 -0.14 -0.37 0.34
N.Valid 32.00 32.00 32.00 32.00 32.00 32.00 32.00 32.00 32.00
N 32.00 32.00 32.00 32.00 32.00 32.00 32.00 32.00 32.00
Pct.Valid 100.00 100.00 100.00 100.00 100.00 100.00 100.00 100.00 100.00
Table: Table continues below
vs wt
----------------- -------- --------
Mean 0.44 3.22
Std.Dev 0.50 0.98
Min 0.00 1.51
Q1 0.00 2.54
Median 0.00 3.33
Q3 1.00 3.65
Max 1.00 5.42
MAD 0.00 0.77
IQR 1.00 1.03
CV 1.15 0.30
Skewness 0.24 0.42
SE.Skewness 0.41 0.41
Kurtosis -2.00 -0.02
N.Valid 32.00 32.00
N 32.00 32.00
Pct.Valid 100.00 100.00
write.csv(summaryData, "summary_data.csv")