這份作業希望能夠讓你熟悉
- R data structure
- conditioning and loop
打好地基之後蓋房子才穩,祝你寫作業愉快!
value
,內容是從 51 到 100 之間的正整數,請利用[]
從中取出質數,但不能用偷懶寫法直接取值 value[51,...]
# (1)
<- c(51:100)
value %%2!=0&value%%3!=0&value%%2!=0&value%%5!=0&value%%7!=0]
value[value### 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
vector_02
變成c(2,3,5)
,並將結果分別存到 vector_03
和 vector_04
裡面。但不能直接使用 assignment 的偷懶寫法 vector_03 <- c(2,3,5)
# (2)
<- c(1,3,5)
vector_02 ### your code
# method01
<- vector_02 + c(1,0,0)
vector_03 # method02
<- vector_02 * c(2,1,1) vector_04
下方列出部分新聞所本學期開設課程,請協助
student_quota
元素,將修課限制人數加倍,並將結果印出,造成老師困擾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)
$teacher
list_journalism### result should be
#> [1] "chen" "hung" "liang" "lee"
# (2)
mean(list_journalism$student_quota)
### result should be
#> [1] 16
# (3)
$student_quota <- list_journalism$student_quota*2
list_journalism$student_quota
list_journalism### result should be
#> [1] 40 30 18 40
# (4)
$weekday <- c("三","三","五","三")
list_journalism$weekday
list_journalism### result should be
#> [1] "三" "三" "五" "三"
#> [1] "chen" "hung" "liang" "lee"
#> [1] 16
#> [1] 40 30 18 40
#> [1] "三" "三" "五" "三"
下方提供 The Economist 所開放的大麥克指數原始資料,已經先篩選出 2021 年的資料。
3706.765
<- read.csv("data/AS01/df_mac_2021.csv")
df_mac
### your code
# (1)
$name=="Taiwan",]
df_mac[df_mac### result should be
#> name local_price GDP_dollar
#> 67 Taiwan 72 25873.37
#> 139 Taiwan 72 28305.92
# (2)
<- df_mac[order(df_mac$GDP_dollar, decreasing = T),]
df_mac_sort $GDP_dollar[2]
df_mac_sort### result should be
#> [1] 83849.81
# (3)
$GDP_dollar==3706.765,]
df_mac[df_mac### 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"
有位同學小軒非常偏食,請依照小軒的飲食禁忌,利用迴圈與條件判斷,印出小軒是否會吃某家餐廳
- 小軒吃:林師傅、馬祖麵館、大李水餃、李記水餃、Barkers美式餐廳
- 小軒不吃:五九麵館、親來食堂、憶馬當鮮、穰穰、師大第一腿
### your code
<- c("Barkers美式餐廳", "親來食堂", "李記水餃", "憶馬當鮮", "穰穰",
vector_shop "五九麵館", "大李水餃", "林師傅", "馬祖麵館", "師大第一腿")
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] "小軒不吃師大第一腿"