作業目的:

這份作業希望能夠讓你熟悉
- R data structure
- conditioning and loop

打好地基之後蓋房子才穩,祝你寫作業愉快!

vector

  1. 有一個 vector 叫做 value,內容是從 51 到 100 之間的正整數,請利用[]從中取出質數,但不能用偷懶寫法直接取值 value[51,...]
# (1)
value <- c(51:100)
value[value%%2!=0&value%%3!=0&value%%2!=0&value%%5!=0&value%%7!=0]
### result should be
#>  [1] 53 59 61 67 71 73 79 83 89 97
#>  [1] 53 59 61 67 71 73 79 83 89 97
  1. 請利用兩種不同的方法,將vector_02變成c(2,3,5),並將結果分別存到 vector_03vector_04 裡面。但不能直接使用 assignment 的偷懶寫法 vector_03 <- c(2,3,5)
# (2)
vector_02 <- c(1,3,5)
### your code
# method01
vector_03 <- vector_02 + c(1,0,0)
# method02
vector_04 <- vector_02 * c(2,1,1)

list

下方列出部分新聞所本學期開設課程,請協助

  1. 抓出教師姓氏
  2. 計算學生人數平均
  3. 修改student_quota元素,將修課限制人數加倍,並將結果印出,造成老師困擾
  4. 在 list 中插入新元素 weekday,內容為 c("三","三","五","三"),並將weekday印出
list_journalism <-
  list(teacher=c("chen","hung","liang","lee"),
       course=c("新聞寫作","全球化與傳播","採訪寫作二","新聞編輯與資訊設計"),
       student_quota=c(20,15,9,20),
       dept="journalism")

### your code
# (1)
list_journalism$teacher
### result should be
#> [1] "chen"  "hung"  "liang" "lee" 
# (2)
mean(list_journalism$student_quota)
### result should be
#> [1] 16
# (3)
list_journalism$student_quota <- list_journalism$student_quota*2
list_journalism$student_quota
### result should be
#> [1] 40 30 18 40
# (4) 
list_journalism$weekday <- c("三","三","五","三")
list_journalism$weekday
### result should be
#> [1] "三" "三" "五" "三"
#> [1] "chen"  "hung"  "liang" "lee"  
#> [1] 16
#> [1] 40 30 18 40
#> [1] "三" "三" "五" "三"

dataframe

下方提供 The Economist 所開放的大麥克指數原始資料,已經先篩選出 2021 年的資料。

  1. 請篩選出台灣的資料並印出 dataframe
  2. 請印出 GDP dollar 的次大值(second largest)
  3. 請找出哪個國家的 GSP_dollar 為 3706.765
  4. 請找出 local_price 曾經 < 2 的所有國家
df_mac <- read.csv("data/AS01/df_mac_2021.csv")

### your code
# (1)
df_mac[df_mac$name=="Taiwan",]
### result should be
#>       name local_price GDP_dollar
#> 67  Taiwan          72   25873.37
#> 139 Taiwan          72   28305.92
# (2)
df_mac_sort <- df_mac[order(df_mac$GDP_dollar, decreasing = T),]
df_mac_sort$GDP_dollar[2]
### result should be
#> [1] 83849.81
# (3)
df_mac[df_mac$GDP_dollar==3706.765,]
### result should be
#>       name local_price GDP_dollar
#> 68 Ukraine          62   3706.765
# (4)
unique(df_mac[df_mac$local_price<2,]$name)
### result should be
#> [1] "Bahrain" "Kuwait"  "Oman"
#>       name local_price GDP_dollar
#> 67  Taiwan          72   25873.37
#> 139 Taiwan          72   28305.92
#> [1] 83849.81
#>       name local_price GDP_dollar
#> 68 Ukraine          62   3706.765
#> [1] "Bahrain" "Kuwait"  "Oman"

for loop & conditional statement

有位同學小軒非常偏食,請依照小軒的飲食禁忌,利用迴圈與條件判斷,印出小軒是否會吃某家餐廳
- 小軒吃:林師傅、馬祖麵館、大李水餃、李記水餃、Barkers美式餐廳
- 小軒不吃:五九麵館、親來食堂、憶馬當鮮、穰穰、師大第一腿

### your code
vector_shop <- c("Barkers美式餐廳", "親來食堂", "李記水餃", "憶馬當鮮", "穰穰",
                 "五九麵館", "大李水餃", "林師傅", "馬祖麵館", "師大第一腿")

for (i in 1:length(vector_shop)) {
  if(vector_shop[i]%in% c("Barkers美式餐廳", "大李水餃", "李記水餃", "林師傅", "馬祖麵館")){
    print(paste0("小軒吃",vector_shop[i]))
  }
  else{
    print(paste0("小軒不吃",vector_shop[i]))
  }
}

### result should be
# [1] "小軒吃Barkers美式餐廳"
# [1] "小軒不吃親來食堂"
# [1] "小軒吃李記水餃"
# [1] "小軒不吃憶馬當鮮"
# [1] "小軒不吃穰穰"
# [1] "小軒不吃五九麵館"
# [1] "小軒吃大李水餃"
# [1] "小軒吃林師傅"
# [1] "小軒吃馬祖麵館"
# [1] "小軒不吃師大第一腿"
#> [1] "小軒吃Barkers美式餐廳"
#> [1] "小軒不吃親來食堂"
#> [1] "小軒吃李記水餃"
#> [1] "小軒不吃憶馬當鮮"
#> [1] "小軒不吃穰穰"
#> [1] "小軒不吃五九麵館"
#> [1] "小軒吃大李水餃"
#> [1] "小軒吃林師傅"
#> [1] "小軒吃馬祖麵館"
#> [1] "小軒不吃師大第一腿"