練習1:
1.\[\begin{align*}3 + 5 &\end{align*}\]
3+5
## [1] 8
2.\[\sqrt{2} \times \sqrt{3}\]
a<- sqrt(2) * sqrt(3)
a
## [1] 2.44949
3.\[(2 \cdot \sqrt{3})^{2.5}\]
a<-(2*sqrt(3))^2.5
a
## [1] 22.33452
練習2:
1. \(1 + 2 + 3 + 4 + \ldots + 10\)
##方法1
sum(1:10)
## [1] 55
##方法2
n <- 10
a <- n * (n + 1) / 2
a
## [1] 55
2.\[\sqrt{2 \cdot \sqrt{3 \cdot 5}}\]
a <- sqrt(2 * sqrt(3 * 5))
a
## [1] 2.783158
3.\[\frac{8 + \sqrt{5^2 - 4 \cdot 2 \cdot 2}}{2 \cdot 3}\]
a<- (8 + sqrt(5^2 - 4 * 2 * 2)) / (2 * 3)
a
## [1] 1.833333
# 計算每年的小時數
hours_per_year <- 365 * 24
hours <- 158567
age_years <- hours / hours_per_year
age_years
## [1] 18.10126
5.活了6574天,還有幾天過生日
# 活了的天數
days_lived <- 6574
# 計算年數(假設一年 365 天)
years_lived <- days_lived / 365
# 取整得到已過的完整年份
complete_years <- floor(years_lived)
# 計算已過的天數
days_passed <- complete_years * 365
# 計算距離下一次生日的天數
days_until_birthday <- 365 - (days_lived - days_passed)
# 查看結果
print(days_until_birthday)
## [1] 361
6.平均壽命81歲,平均活幾天、或幾小時
# 平均壽命
average_lifespan_years <- 81
# 計算平均活的天數
average_days <- average_lifespan_years * 365
# 計算平均活的總小時數
average_hours <- average_days * 24
# 查看平均天數
average_days
## [1] 29565
##常看平均時數
average_hours
## [1] 709560
練習簡單的數學運算
●直接輸入計算下列函數的值
1.\[\frac{1}{\sqrt{2\pi}} e^{\frac{(1 -
0)^2}{-2}}\]
a <- (1 / sqrt(2 * pi)) * exp((1 - 0)^2 / -2)
a
## [1] 0.2419707
2.\[\frac{1}{\sqrt{2\pi}} e^{\frac{(2 - 0)^2}{-2}}\]
a <- (1 / sqrt(2 * pi)) * exp((2 - 0)^2 / -2)
a
## [1] 0.05399097
3.\[\frac{e^{-2} \cdot 2^5}{5!}\] where 5! = 5×4×3×2×1
# 計算 e 的 -2 次方
e2 <- exp(-2)
# 計算 2 的 5 次方
two_5 <- 2^5
# 計算 5 的階乘
factorial_5 <- factorial(5)
# 計算公式的結果
a <- (e2 * two_5) / factorial_5
a
## [1] 0.03608941
4.\[\frac{e^{-2} \cdot 2^3}{3!}\]
# 計算 e 的 -2 次方
e2 <- exp(-2)
# 計算 2 的 3 次方
two_3 <- 2^3
# 計算 3 的階乘
factorial_3 <- factorial(3)
# 計算公式的結果
a <- (e2 * two_3) / factorial_3
a
## [1] 0.180447
# 設定圓的半徑
radius <- 5
# 計算圓的面積
area <- pi * radius^2
area
## [1] 78.53982
# 計算 log(2) + log(3)
result_addition <- log(2) + log(3)
# 計算 log(6)
result_log6 <- log(6)
# 查看結果
result_addition
## [1] 1.791759
result_log6
## [1] 1.791759
練習
○直接輸入計算下列函數的值
○ x_1=1 \[\frac{1}{\sqrt{2\pi}} e^{\frac{(x_1
- 0)^2}{-2}}\]
x<-1
a <- (1 / sqrt(2 * pi)) * exp((x - 0)^2 / -2)
a
## [1] 0.2419707
○ x_1=2 \[\frac{1}{\sqrt{2\pi}} e^{\frac{(x_1 - 0)^2}{-2}}\]
x<-2
a <- (1 / sqrt(2 * pi)) * exp((x - 0)^2 / -2)
a
## [1] 0.05399097
○ x.2=5 \[\frac{e^{-2} \cdot 2^(x \cdot 2)}{x \cdot 2!}\]
x.2=5
e2 <- exp(-2)
# 計算 2 的 x.2 次方
two_5 <- 2^x.2
# 計算 x.2的階乘
factorial_5 <- factorial(x.2)
# 計算公式的結果
a <- (e2 * two_5) / factorial_5
a
## [1] 0.03608941
○ x.2=3 \[\frac{e^{-2} \cdot 2^(x \cdot 2)}{x \cdot 2!}\]
x.2=3
e2 <- exp(-2)
# 計算 2 的 x.2 次方
two_5 <- 2^x.2
# 計算 x.2 的階乘
factorial_5 <- factorial(x.2)
# 計算公式的結果
a <- (e2 * two_5) / factorial_5
a
## [1] 0.180447
練習
○ x<–5,判斷x是正值或負值
# 設定 x 的值
x <- -5
# 使用 cat() 來顯示結果
cat(x, "是", ifelse(x %% 2 == 0, "偶數", "奇數"), "\n")
## -5 是 奇數
○ 把邏輯判斷設為整數—正值輸出1,負值輸出0
# 設定 x 的值
x <- -5
# 判斷 x 是正值還是負值
result <- as.integer(x > 0) # 如果 x > 0,則 result 為 1;否則為 0
# 輸出結果
cat("結果是:", result, "\n")
## 結果是: 0
○ 正值輸出1,負值輸出-1
# 設定 x 的值
x <- -5
# 判斷 x 是正值還是負值
result <- ifelse(x > 0, 1, -1) # 如果 x > 0,則 result 為 1;否則為 -1
# 輸出結果
cat("結果是:", result, "\n")
## 結果是: -1
HW1: 自己寫一個絕對值運算
○ 計算
○ x<–10
○ abs(x)=10
# 設定 x 的值
x <- -10
# 計算絕對值
abs_value <- abs(x)
# 輸出結果
cat("x 的絕對值是:", abs_value, "\n")
## x 的絕對值是: 10
練習
○ 還有幾天放寒假
# 設定今天的日期和寒假開始的日期
today <- as.Date("2024-10-24")
winter_break_start <- as.Date("2025-01-01")
# 計算還有幾天
days_until_winter_break <- as.numeric(winter_break_start - today)
# 輸出結果
cat("還有", days_until_winter_break, "天放寒假。\n")
## 還有 69 天放寒假。
練習
○ 寫出一個向量:1, 2, 3, 10, 11, 12, 1, 2, 3, 10, 11, 12
# 創建向量
a <- c(1, 2, 3, 10, 11, 12, 1, 2, 3, 10, 11, 12)
# 輸出向量
a
## [1] 1 2 3 10 11 12 1 2 3 10 11 12
○ 輸入向量3,2,1
a <- c(3, 2, 1)
a
## [1] 3 2 1
練習
○ 設X為1:100的向量
x=c(1:100)
x
## [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
○ 求X的總和
sum(x)
## [1] 5050
○ 輸出X向量的第1個與第100元素的值
# 輸出第 1 個和第 100 個元素的值
x1 <- x[1]
x100 <- x[100]
# 顯示結果
cat("X 的第 1 個元素是:", x1, "\n")
## X 的第 1 個元素是: 1
cat("X 的第 100 個元素是:", x100, "\n")
## X 的第 100 個元素是: 100
○ 求X中>50的元素個數
# 計算 X 中大於 50 的元素個數
count_greater_than_50 <- sum(x > 50)
# 輸出結果
cat("X 中大於 50 的元素個數是:", count_greater_than_50, "\n")
## X 中大於 50 的元素個數是: 50
練習
○ 產生向量,2,4, …100, 2,4, …100
# 創建一個從 2 到 100 的偶數向量
even_numbers <- seq(2, 100, by = 2)
# 重複這個向量
v <- c(even_numbers, even_numbers)
# 輸出結果
v
## [1] 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36
## [19] 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72
## [37] 74 76 78 80 82 84 86 88 90 92 94 96 98 100 2 4 6 8
## [55] 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44
## [73] 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80
## [91] 82 84 86 88 90 92 94 96 98 100
○ 輸出資料長度
# 創建一個從 2 到 100 的偶數向量並重複
even_numbers <- seq(2, 100, by = 2)
my_vector <- c(even_numbers, even_numbers)
# 計算向量的長度
vector_length <- length(my_vector)
# 輸出長度
cat("向量的長度是:", vector_length, "\n")
## 向量的長度是: 100
○ 計算和、平均數、累加向量
# 創建一個從 2 到 100 的偶數向量並重複
even_numbers <- seq(2, 100, by = 2)
my_vector <- c(even_numbers, even_numbers)
# 計算向量的和
total_sum <- sum(my_vector)
# 計算向量的平均數
average_value <- mean(my_vector)
# 計算累加向量
cumulative_sum <- cumsum(my_vector)
# 輸出結果
cat("向量的和是:", total_sum, "\n")
## 向量的和是: 5100
cat("向量的平均數是:", average_value, "\n")
## 向量的平均數是: 51
cat("累加向量是:", cumulative_sum, "\n")
## 累加向量是: 2 6 12 20 30 42 56 72 90 110 132 156 182 210 240 272 306 342 380 420 462 506 552 600 650 702 756 812 870 930 992 1056 1122 1190 1260 1332 1406 1482 1560 1640 1722 1806 1892 1980 2070 2162 2256 2352 2450 2550 2552 2556 2562 2570 2580 2592 2606 2622 2640 2660 2682 2706 2732 2760 2790 2822 2856 2892 2930 2970 3012 3056 3102 3150 3200 3252 3306 3362 3420 3480 3542 3606 3672 3740 3810 3882 3956 4032 4110 4190 4272 4356 4442 4530 4620 4712 4806 4902 5000 5100
練習
○ 活了34025天,目前是實歲幾歲(假設一年365天不計任年)
虛歲幾歲?假設生日是9/1
# 定義活了的天數
days_alive <- 34025
# 計算實歲
actual_age <- days_alive %/% 365
# 計算虛歲
virtual_age <- actual_age + 1
# 輸出結果
cat("實歲:", actual_age, "歲\n")
## 實歲: 93 歲
cat("虛歲:", virtual_age, "歲\n")
## 虛歲: 94 歲
○ 還有幾天過生日
# 設定當前日期
today_date <- Sys.Date()
# 確認當前日期
print(paste("今天的日期是:", today_date))
## [1] "今天的日期是: 2024-10-24"
# 設定今年的生日
current_year <- as.numeric(format(today_date, "%Y")) # 獲取當前年份
birthday_this_year <- as.Date(paste(current_year, "09", "01", sep = "-"))
# 顯示今年的生日
print(paste("今年的生日是:", birthday_this_year))
## [1] "今年的生日是: 2024-09-01"
if (today_date < birthday_this_year) {
next_birthday <- birthday_this_year
} else {
next_birthday <- as.Date(paste(current_year + 1, "09", "01", sep = "-"))
}
days_until_next_birthday <- as.numeric(next_birthday - today_date)
cat("距離下次生日還有", days_until_next_birthday, "天。\n")
## 距離下次生日還有 312 天。
HW2:
○ 計算下面函數的值(二項分配機率值)
n=20;p=0.5; x=c(0, 1, 2)
probabilities <- dbinom(x, size = n, prob = p)
probabilities
## [1] 9.536743e-07 1.907349e-05 1.811981e-04
○ 計算下面函數的累加數值(二項分配累積機率值)
cumulative_probability <- pbinom(x, size = n, prob = p)
cumulative_probability
## [1] 9.536743e-07 2.002716e-05 2.012253e-04
HW 3
○ x為1-100的向量,求最大值與最小值
# 設定 x 為 1 到 100 的向量
x <- 1:100
# 計算最大值與最小值
max_value <- max(x)
min_value <- min(x)
# 顯示結果
cat("最大值是:", max_value, "\n")
## 最大值是: 100
cat("最小值是:", min_value, "\n")
## 最小值是: 1
○ 刪除x向量中,最小與最大兩個元素
x_modified <- x[-c(max(x), min(x))]
x_modified
## [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] 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
## [51] 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
## [76] 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
HW 4 :
○ 計算標準常態分配函數值:x=0, 1, 2 , 3, 4, 5, 6, 7, 8
# 設定參數
mu <- 0 # 均值
sigma <- 2 # 標準差
x <- 0:8 # x 的範圍
# 計算標準常態分配函數值
f_x <- (1 / (sqrt(2 * pi) * sigma)) * exp(-((x - mu)^2) / (2 * sigma^2))
data.frame(x = x, f_x = f_x)
## x f_x
## 1 0 1.994711e-01
## 2 1 1.760327e-01
## 3 2 1.209854e-01
## 4 3 6.475880e-02
## 5 4 2.699548e-02
## 6 5 8.764150e-03
## 7 6 2.215924e-03
## 8 7 4.363413e-04
## 9 8 6.691511e-05
○ 計算下面函數的值(二項分配機率值),
n=20, p=0.5, x=0, 1, 2 , 3, 4, 5, 6, 7, 8, 9, 10
# 設定參數
n <- 20 # 試驗次數
p <- 0.5 # 成功的機率
x <- 0:10 # x 的取值範圍
# 計算二項分配機率
probabilities <- dbinom(x, size = n, prob = p)
# 顯示結果
data.frame(x = x, Probability = probabilities)
## x Probability
## 1 0 9.536743e-07
## 2 1 1.907349e-05
## 3 2 1.811981e-04
## 4 3 1.087189e-03
## 5 4 4.620552e-03
## 6 5 1.478577e-02
## 7 6 3.696442e-02
## 8 7 7.392883e-02
## 9 8 1.201344e-01
## 10 9 1.601791e-01
## 11 10 1.761971e-01
○ 計算下面函數的累加數值(二項分配累積機率值),
n=20, p=0.5, x=0, 1, 2 , 3, 4, 5, 6, 7, 8, 9, 10
# 設定參數
n <- 20 # 試驗次數
p <- 0.5 # 成功的機率
x <- 0:10 # x 的取值範圍
# 計算二項分配累積機率
cumulative_probabilities <- pbinom(x, size = n, prob = p)
# 顯示結果
data.frame(x = x, Cumulative_Probability = cumulative_probabilities)
## x Cumulative_Probability
## 1 0 9.536743e-07
## 2 1 2.002716e-05
## 3 2 2.012253e-04
## 4 3 1.288414e-03
## 5 4 5.908966e-03
## 6 5 2.069473e-02
## 7 6 5.765915e-02
## 8 7 1.315880e-01
## 9 8 2.517223e-01
## 10 9 4.119015e-01
## 11 10 5.880985e-01