Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Martin老師
    @tpemartin
    給你一個提示是第一次期中考
    實際上我相信很多同學題目寫不出來有一部分在於他們沒有去看錯誤提示所給的有用訊息。我會鼓勵同學從今天開始一看錯誤訊息試著了解他的意思是什麼。
    lyx231
    @lyx231
    when_busArrives<-function(route,stop){
    library(httr)
    library(magrittr)
    library(purrr)
    library(stringr)
    GET("https://ptx.transportdata.tw/MOTC/v2/Bus/EstimatedTimeOfArrival/City/NewTaipei/916?$top=30&$format=JSON") %>%
      content -> estimatedArrivalTime_916
    
    GET("https://ptx.transportdata.tw/MOTC/v2/Bus/Route/City/NewTaipei/939?$top=30&$format=JSON") %>%
      content -> route_916
    
    as.integer(map(estimatedArrivalTime_916,~.x$StopName$Zh_tw==stop))+
    as.integer(map(estimatedArrivalTime_916,~.x$RouteName$Zh_tw==route))->first
    
    estimatedArrivalTime_916[first==2] ->second
    
    unlist(map(second,~.x$Direction))->three
    
    estimatedArrivalTime<-list(
      `去程`=list(time=second[three==0][[1]]$EstimateTime),
      `返程`=list(time=second[three==1][[1]]$EstimateTime)
    )
    paste0(estimatedArrivalTime$`去程`$time,"秒")->estimatedArrivalTime$`去程`$time
    paste0(estimatedArrivalTime$`返程`$time,"秒")->estimatedArrivalTime$`返程`$time
    
    if(estimatedArrivalTime$`去程`$time=="秒"){
        estimatedArrivalTime$`去程`$time<-"未發車"
    }
    if(estimatedArrivalTime$`返程`$time=="秒"){
          estimatedArrivalTime$`返程`$time<-"未發車"
    }
    
    return(estimatedArrivalTime)
    }
    
    when_busArrives("916","臺北大學正門")
    3 replies
    greening6
    @greening6
    askSilly_weather <- function(locationInput,dateInput){
      if(!require("jsonlite")){install.packages("jsonlite")}
      if(str_detect(locationInput,"台")){str_replace(locationInput,"台|臺") -> locationInput}
      jsonlite::fromJSON("https://opendata.cwb.gov.tw/fileapi/v1/opendataapi/F-C0032-001?Authorization=rdec-key-123-45678-011121314&format=JSON") ->
      weather_next36hours
    
      (weather_next36hours$cwbopendata$dataset$location$locationName == locationInput) -> pick_location
    
      weather_next36hours$cwbopendata$dataset$location$weatherElement[pick_location][[1]] -> weatherAtLocation
      (weatherAtLocation$elementName=="MaxT") ->
        pick_MaxT
      (weatherAtLocation$elementName=="MinT") ->
        pick_MinT
      weatherAtLocation$time[pick_MaxT][[1]]$parameter$parameterName[[1]] -> maxT
      weatherAtLocation$time[pick_MinT][[1]]$parameter$parameterName[[1]] -> minT
    
      glue::glue("{locationInput} {dateInput} 氣溫,最高{maxT}度,最低{minT}度。")
    }
    MelodyLin0106
    @MelodyLin0106
    check_package <- function(pkgName){
      if(!require(pkgName)){ 
      install.packages(pkgName)
      }
      }
    emilyluckey
    @emilyluckey

    請問老師,為什麼我再跑studentid時的warning跑出來的都是�A�ҿ�J���Ǹ������T

    warning無法顯示中文的問題已經在新版的rstudio更新了,有需要的同學可以去rstudio官網下載更新,步驟和第一次安裝rstudio一樣不用多作甚麼處理

    Martin老師
    @tpemartin
    真棒的消息。
    greening6
    @greening6
    check_package <- function(pkgName){if(!require("pkgName")){install.packages("pkgName")}}
    5 replies
    emilyluckey
    @emilyluckey
    因為還蠻多人問的所以統一說明一次,在function裡面會把放在function(){}()裡面的東西視為一個物件,你可以想像成這個你設計的function在執行時他會先做「放在()裡面的東西」->pkgName,然後再用pkgName去繼續跑後面{}裡面的東西
    所以如果你今天先做"是在哈樓" -> a,然後再做"a"會發生甚麼事呢~?
    jack860920
    @jack860920
    check_package<- function(pkgName){
    if(!require(pkgName)){install.packages(pkgName)}
    }
    Martin老師
    @tpemartin
    有不少同學還是對於pkgName與"pkgName"的差別無法理解,老師舉個早期喜劇的內容。A指著前方說 有間客棧,B一看,咦那個客棧還真的叫 "有間客棧"; "有間客棧"是定值,但 有間客棧 要看你說的是哪一間,它不一定叫 "有間客棧",它可能是"西華"也可能是"福華". 有間客棧 是 pkgName, 而 "有間客棧" 是 "pkgName" .
    函數要對任何客棧都能檢視,那body就必需要放 有間客棧 而不能放 "有間客棧"。
    Martin老師
    @tpemartin
    FredaHsieh
    @FredaHsieh
    check_package <- function(pkgName){
      if(!require(pkgName)){ 
      install.packages(pkgName)
      }
      }
    Ulrixon
    @Ulrixon
    compute_letterGrade=function(myGrade){
      condition=ifelse(myGrade<=69,"(-1,69]",ifelse(myGrade<=79,"(69,79]"
                                                    ,ifelse(myGrade<=89,"(79,89]",ifelse(myGrade>=100,"W","(89,100]"))))
      switch (condition,
        "(-1,69]"={
        print("-_-")
        "F"},
      "(69,79]"={
        print("棒")
        "C"
        },
      "(79,89]"={
        print("好棒")
        "B"
        },
      "(89,100]"={
        print("好棒棒")
        "A"
      },
      "W"={
        print("incorrect grade number inputed")
        "W"
      }
    
      )-> letterGrade
      return(letterGrade)
    }
    1 reply
    Ulrixon
    @Ulrixon
    compute_letterGrade=function(myGrade){
      condition=cut(myGrade,c(-1,69,79,89,100))
      switch (as.character(condition),
        "(-1,69]"={
        print("-_-")
        "F"},
      "(69,79]"={
        print("棒")
        "C"
        },
      "(79,89]"={
        print("好棒")
        "B"
        },
      "(89,100]"={
        print("好棒棒")
        "A"
      },
      {
        print("incorrect grade number inputed")
        "W"
      }
    
      )-> letterGrade
      return(letterGrade)
    }
    1 reply
    Ulrixon
    @Ulrixon
    whatNeedToDo_butler_mom <- function(nowtime=now()){
      library(lubridate)
      library(glue)
    
      cutpoints <-
        c(
          glue("{today()} 06:00:00"),
          glue("{today()} 09:00:00"),
          glue("{today()} 17:00:00"),
          glue("{today()} 19:00:00"),
          glue("{today()} 23:30:00"),
          glue("{today()+days(1)} 01:00:00")
        )
      cutpoints %>%
        ymd_hms(tz="Asia/Taipei") -> cutpoints
    
      cut(nowtime, cutpoints) -> nowtime
      levels(nowtime)[c(1,3,5)] <-
        c("morning","evening","jazz time")
      nowtime=as.character(nowtime)
      if(nowtime=="morning"){
          print("要不要來閱讀點國際時事?")
          browseURL("https://economist.com")
        }else if(nowtime=="evening"){
          print("需不需要點餐呢?")
          browseURL("https://www.foodpanda.com.tw/")
        }else if(nowtime=="jazz time"){
          print("放鬆聽點Jazz。")
          browseURL("https://open.spotify.com/playlist/37i9dQZF1DXbITWG1ZJKYt")
        }else{
          print("機器人要休息,饒了我吧~~")
        }
    
    
    }
    1 reply
    
    whatNeedToDo_butler_dad <- function(nowtime=now()){
      library(lubridate)
      library(glue)
    
      cutpoints <-
        c(
          glue("{today()} 06:00:00"),
          glue("{today()} 09:00:00"),
          glue("{today()} 17:00:00"),
          glue("{today()} 19:00:00"),
          glue("{today()} 23:30:00"),
          glue("{today()+days(1)} 01:00:00")
        )
      cutpoints %>%
        ymd_hms(tz="Asia/Taipei") -> cutpoints
    
      cut(nowtime, cutpoints) -> nowtime
      levels(nowtime)[c(1,3,5)] <-
        c("morning","evening","jazz time")
      nowtime=as.character(nowtime)
      if(nowtime=="morning"){
          print("要不要來閱讀點國際時事?")
          browseURL("https://www.wsj.com/")
        }else if(nowtime=="evening"){
          print("需不需要規劃週末全家旅行呢?")
          browseURL("https://www.tripadvisor.com/")
        }else if(nowtime=="jazz time"){
          print("機器人要休息,饒了我吧~~")
    
        }else{
          print("機器人要休息,饒了我吧~~")
        }
    
    
    }
    1 reply
    Ulrixon
    @Ulrixon
    
    whatNeedToDo_butler_ming <- function(nowtime=now()){
      library(lubridate)
      library(glue)
    
      cutpoints <-
        c(
          glue("{today()} 06:00:00"),
          glue("{today()} 09:00:00"),
          glue("{today()} 17:00:00"),
          glue("{today()} 19:00:00"),
          glue("{today()} 23:30:00"),
          glue("{today()+days(1)} 01:00:00")
        )
      cutpoints %>%
        ymd_hms(tz="Asia/Taipei") -> cutpoints
    
      cut(nowtime, cutpoints) -> nowtime
      levels(nowtime)[c(1,3,5)] <-
        c("morning","evening","jazz time")
      nowtime=as.character(nowtime)
      if(nowtime=="morning"){
          print("檢查書包東西都有帶嗎?")
    
        }else if(nowtime=="evening"){
          print("作業寫好了嗎?")
    
        }else if(nowtime=="jazz time"){
          print("機器人要休息,饒了我吧~~")
    
        }else{
          print("機器人要休息,饒了我吧~~")
        }
    
    
    }
    1 reply
    Ulrixon
    @Ulrixon
    grading=function(myAnswer, correctAnswer){
      a=0
      if(all(myAnswer == correctAnswer)&&class(myAnswer)==class(correctAnswer)){ # 要全TRUE
      a=10
      }
      return(a)
    }
    Ulrixon
    @Ulrixon
    Ufun=function(a1, a2, m, alpha=0.5){
      a=(a1**alpha)*(a2**(1-alpha))-m
      return(a)
    }
    Ulrixon
    @Ulrixon
    whatNeedToDo_butler_mom <- function(nowtime=now()){
      library(lubridate)
      library(glue)
      library(magrittr)
    
      cutpoints <-
        c(
          glue("{today()} 06:00:00"),
          glue("{today()} 09:00:00"),
          glue("{today()} 17:00:00"),
          glue("{today()} 19:00:00"),
          glue("{today()} 23:30:00"),
          glue("{today()+days(1)} 01:00:00")
        )
      cutpoints %>%
        ymd_hms(tz="Asia/Taipei") -> cutpoints
    
    
    
      switch(
        nowtime=dplyr::case_when(
    
      cutpoints[[1]]<=nowtime && nowtime <= cutpoints[[2]] ~"morning",
      cutpoints[[3]]<=nowtime && nowtime <= cutpoints[[4]] ~"evening",
      cutpoints[[5]]<=nowtime && nowtime<= cutpoints[[6]] ~"jazz time"
    
    ),
        "morning"={
          print("要不要來閱讀點國際時事?")
          browseURL("https://economist.com")
        },
        "evening"={
          print("需不需要點餐呢?")
          browseURL("https://www.foodpanda.com.tw/")
        },
        "jazz time"={
          print("放鬆聽點Jazz。")
          browseURL("https://open.spotify.com/playlist/37i9dQZF1DXbITWG1ZJKYt")
        },
        {
          print("機器人要休息,饒了我吧~~")
        }
      )
    
    
    }
    raytang1758
    @raytang1758
    whatNeedToDo_butler_ming <- function(nowtime=now()){
      cutpoints <-
        c(
          glue("{today()} 06:00:00"),
          glue("{today()} 09:00:00"),
          glue("{today()} 17:00:00"),
          glue("{today()} 19:00:00")
        )
        cutpoints %>%
        ymd_hms(tz="Asia/Taipei") -> cutpoints
    
      cut(nowtime, cutpoints) -> nowtime
      levels(nowtime)[c(1,3)] <-
        c("morning","evening")
    
     if(nowtime=="morning"){
          print("檢查書包東西都有帶嗎?")
        }else if(nowtime=="evening"){
          print("作業寫好了嗎?")
        }else{
          print("機器人要休息,饒了我吧~~")
        }
    }
    ymd_hms(glue::glue("{today()} 08:00:00"),tz="Asia/Taipei") %>%
      whatNeedToDo_butler_ming(.)
    1 reply
    greening6
    @greening6
    whatNeedToDo_butler_dad <- function(nowtime=now()){
      library(lubridate)
      library(glue)
    
      cutpoints <-
        c(
          glue("{today()} 06:00:00"),
          glue("{today()} 09:00:00"),
          glue("{today()} 17:00:00"),
          glue("{today()} 19:00:00"),
          glue("{today()} 23:30:00"),
          glue("{today()+days(1)} 01:00:00")
        )
      cutpoints %>%
        ymd_hms(tz="Asia/Taipei") -> cutpoints
    
      cut(nowtime, cutpoints) -> nowtime
      levels(nowtime)[c(1,3,5)] <-
        c("morning","evening","jazz time")
    
      switch(
        as.character(nowtime),
        "morning"={
          print("要不要來閱讀點國際新聞呢?")
          browseURL("https://www.wsj.com/")
        },
        "evening"={
          print("需不需瀏覽旅遊景點?")
          browseURL("https://www.tripadvisor.com/")
        },
        {
          print("機器人要休息,饒了我吧~~")
        }
      )
    }
    
    ymd_hms(glue::glue("{today()} 08:00:00"),tz="Asia/Taipei") %>%
      whatNeedToDo_butler_dad()
    
    ymd_hms(glue::glue("{today()} 14:00:00"),tz="Asia/Taipei") %>%
      whatNeedToDo_butler_dad()
    
    ymd_hms(glue::glue("{today()} 18:00:00"),tz="Asia/Taipei") %>%
      whatNeedToDo_butler_dad()
    
    ymd_hms(glue::glue("{today()+days(1)} 00:20:00"),tz="Asia/Taipei") %>%
      whatNeedToDo_butler_dad()
    whatNeedToDo_butler_ming <- function(nowtime=now()){
      library(lubridate)
      library(glue)
    
      cutpoints <-
        c(
          glue("{today()} 06:00:00"),
          glue("{today()} 09:00:00"),
          glue("{today()} 17:00:00"),
          glue("{today()} 19:00:00"),
          glue("{today()} 23:30:00"),
          glue("{today()+days(1)} 01:00:00")
        )
      cutpoints %>%
        ymd_hms(tz="Asia/Taipei") -> cutpoints
    
      cut(nowtime, cutpoints) -> nowtime
      levels(nowtime)[c(1,3,5)] <-
        c("morning","evening","jazz time")
    
      switch(
        as.character(nowtime),
        "morning"={
          print("檢查書包東西都有帶嗎?")
        },
        "evening"={
          print("作業寫好了嗎?")
        },
        {
          print("機器人要休息,饒了我吧~~")
        }
      )
    }
    
    ymd_hms(glue::glue("{today()} 08:00:00"),tz="Asia/Taipei") %>%
      whatNeedToDo_butler_ming()
    
    ymd_hms(glue::glue("{today()} 14:00:00"),tz="Asia/Taipei") %>%
      whatNeedToDo_butler_ming()
    
    ymd_hms(glue::glue("{today()} 18:00:00"),tz="Asia/Taipei") %>%
      whatNeedToDo_butler_ming()
    
    ymd_hms(glue::glue("{today()+days(1)} 00:20:00"),tz="Asia/Taipei") %>%
      whatNeedToDo_butler_ming()
    greening6
    @greening6
    grading <- function(myAnswer, correctAnswer){
      if(all(myAnswer == correctAnswer)&class(myAnswer)==class(correctAnswer)){ 
        10
        }
      else{
        0
        }
    }
    Catherine3110
    @Catherine3110
    whatNeedToDo_butler_ming <- function(nowtime=now()){
      library(lubridate)
      library(magrittr)
    cutpoints <-
      c(
        glue("{today()} 06:00:00"),
        glue("{today()} 09:00:00"),
        glue("{today()} 17:00:00"),
        glue("{today()} 19:00:00"),
        glue("{today()} 23:30:00"),
        glue("{today()+days(1)} 01:00:00")
       )
    cutpoints %>%
      ymd_hms(tz="Asia/Taipei") -> cutpoints
    
    cut(nowtime, cutpoints) -> nowtime
    nowtime=dplyr::case_when(
      cutpoints[[1]] <= nowtime & nowtime <= cutpoints[[2]] ~"morning",
      cutpoints[[3]] <= nowtime & nowtime <= cutpoints[[4]] ~"evening",
      cutpoints[[5]] <= nowtime & nowtime <= cutpoints[[6]] ~"jazz time"
    )
    
    if(nowtime=="morning"){
      print("檢查書包東西都有帶嗎?")
     }else if(nowtime=="evening"){
        print("作業寫好了嗎?")
     }else if(nowtime=="jazz time"){
        print("機器人要休息,饒了我吧~~")
     }else{
        print("機器人要休息,饒了我吧~~")
        }
    }
    ymd_hms(glue::glue("{today()} 14:00:00"),tz="Asia/Taipei") %>%
         whatNeedToDo_butler_ming()
    yutungzz
    @yutungzz
    #爸爸
    whatNeedToDo_butler_mom <- function(nowtime = now()) {
        library(lubridate)
        library(glue)
    
        cutpoints <- c(glue("{today()} 06:00:00"), glue("{today()} 09:00:00"), glue("{today()} 17:00:00"), glue("{today()} 19:00:00"))
    cutpoints <- cutpoints %>% ymd_hms(tz = "Asia/Taipei")
    
        nowtime <- cut(nowtime, cutpoints)
        levels(nowtime)[c(1, 3)] <- c("morning", "evening")
    
       switch(as.character(nowtime), morning = {
        print("要不要來閱讀點國際時事?")
        browseURL("https://economist.com")
    }, evening = {
        print("需不需景點推薦呢?")
        browseURL("https://www.tripadvisor.com/")
    }, {
        print("機器人要休息,饒了我吧~~")
    })
    }
    
    ymd_hms(glue::glue("{today()} 08:00:00"), tz = "Asia/Taipei") %>% whatNeedToDo_butler_mom()
    
    ymd_hms(glue::glue("{today()} 14:00:00"), tz = "Asia/Taipei") %>% whatNeedToDo_butler_mom()
    
    ymd_hms(glue::glue("{today()} 18:00:00"), tz = "Asia/Taipei") %>% whatNeedToDo_butler_mom()
    
    ymd_hms(glue::glue("{today()+days(1)} 00:20:00"), tz = "Asia/Taipei") %>% whatNeedToDo_butler_mom()
    #小明
    whatNeedToDo_butler_mom <- function(nowtime = now()) {
        library(lubridate)
        library(glue)
    
        cutpoints <- c(glue("{today()} 06:00:00"), glue("{today()} 09:00:00"), glue("{today()} 17:00:00"), glue("{today()} 19:00:00"))
    cutpoints <- cutpoints %>% ymd_hms(tz = "Asia/Taipei")
    
        nowtime <- cut(nowtime, cutpoints)
        levels(nowtime)[c(1, 3)] <- c("morning", "evening")
    
       switch(as.character(nowtime), morning = {
        print("檢查書包東西都有帶嗎?")
    }, evening = {
        print("作業寫好了嗎?")
    }, {
        print("機器人要休息,饒了我吧~~")
    })
    }
    
    ymd_hms(glue::glue("{today()} 08:00:00"), tz = "Asia/Taipei") %>% whatNeedToDo_butler_mom()
    
    ymd_hms(glue::glue("{today()} 14:00:00"), tz = "Asia/Taipei") %>% whatNeedToDo_butler_mom()
    
    ymd_hms(glue::glue("{today()} 18:00:00"), tz = "Asia/Taipei") %>% whatNeedToDo_butler_mom()
    
    ymd_hms(glue::glue("{today()+days(1)} 00:20:00"), tz = "Asia/Taipei") %>% whatNeedToDo_butler_mom()
    username123242
    @username123242
    factor(covid19$縣市)%>%
    levels(.) %>%
    population[.] %>%
    sort(decreasing=T)%>%
    names(.)%>%
    factor(
        covid19$縣市, levels=.
      )%>%
    levels(.)
    請老師過目
    Martin老師
    @tpemartin

    老師注意到有不少同學在有else或else if的使用上有嚴重錯誤,如:

    choice <- 10
    if (choice < 5) "多消費蛋糕、少消費甜甜圈"
    else if (choice > 5) "少消費蛋糕、多消費甜甜圈"
    else "維持現有消費組合"

    老師增加了5.3.6斷句一小節。原則上有else的地方它的左側就一定要有, 即:

    • } else

    • } else if

    使用上,老師衷心希望你們follow我講義寫法,這是正統寫法比較不會成為不良習慣。

    Martin老師
    @tpemartin

    順便提一下,{}若有分行,body習慣內縮一個tab或空白2格,如果body裡又出現有分行的{},那nest在裡面的body再內縮一個 tab(總共會是2個tab). 如果你的斷句是使用正統斷法, RStudio會自動幫你創造適當內縮。(換句話說,如果RStudio沒自動幫你內縮,你大概不是使用正統斷句習慣。)

    if(){
      # 內縮1個tab
      if(){
        # 內縮2個tab
      }
    }

    It's nice to be orthodox in programming. Cheers.

    selina3067
    @selina3067
    老師 想請問一下考試的時候可不可以用隨身碟帶自己的筆記(習慣用word之類的東西做的自己的筆記~)
    2 replies
    Martin老師
    @tpemartin
    不可以,請見諒。
    Martin老師
    @tpemartin
    gitter及hypothes.is因有線上訊息傳遞作用也不得不關掉,請大家多熟悉自己的筆記。
    Martin老師
    @tpemartin

    同學問題:

    謝謝老師,想請問3-2為什麼我的程式碼的回圈不會跑

    list_availableSeats <- function(seatingChart){
      for(a in 1:7){
        for (b in 1:9){
          if(seatingChart[[a]][[b]] == 0){
            paste0(c("row","-"),c(a,b))
          }
        }
      }
    }
    list_availableSeats(seatingChart)

    謝謝老師

    老師回答:
    https://vimeo.com/ntpuecon/review/433529086/eaf760786c

    Martin老師
    @tpemartin
    老師這裡的Vimeo連結允許同學直接在影片不懂處留言發問,它會直接在影片滑鼠點的位置留一張便條紙留言,很方便大家聚焦討論。
    Martin老師
    @tpemartin
    希望大家多把自己「錯」的程式放上來一起討論。
    selina3067
    @selina3067
    那個網站是不是不能放完整的程式碼(?) 還是我其實有成功放上去
    # list_availableSeats
    
    list_availableSeats<- function(seatingChart){
    seat_available<-c()
    
    for(y in 1:7){map(1:9,~{
      if(seatingChart[[y]][[.x]]==0){
        paste0("row",y,"-",.x)
      }
    }
    )->seat_available[[y]]
      }
    
    #刪掉怪東西
    for(i in 1:7){
      {seat_available[[i]][c(3,8)]<-NULL}
    }
    
    }
    list_availableSeats(seatingChart)
    10 replies
    重留一下我的問題:為什麼我覺得我寫的東西超怪 我明明寫要等於0才能丟進新的vector裡面 但他什麼都丟進去了 ....讓我另外寫一個刪除Null的一行把它刪掉
    selina3067
    @selina3067
    另外一個問題 加了function後我測試最後一行時麼東西都跑不出來(?)
    yeelin-chen
    @yeelin-chen
    Error in Ufun(.x) : 缺少引數 "a2",也沒有預設值 老師請問我這個為什麼會出現這個錯誤
    #1.1
    Ufun<-function(a1, a2, m, alpha=0.5){
      a1^alpha*a2^(1-alpha)-m
    }
    
    
    #1.2
    Apps_10 <- list(
      c(0.38,0.74,0.19), c(0.39,0.46,0.26),
      c(0.56,0.3,0.32), c(0.54,0.31,0.40), c(0.90,0.56,0.43), c(0.39,0.13,0.23), 
      c(0.83,0.74,0.39), c(0.66,0.84,0.37), c(0.41,0.49,0.35),
      c(0.49,0.35,0.20))
    map(Apps_10,~ Ufun(.x))
    3 replies
    Martin老師
    @tpemartin
    針對 @yeelin-chen 的問題(https://gitter.im/ntpuecon/course-108-2-programming-for-data-science?at=5efaf6d03a0d3931fac19427) 老師錄了一個解說,裡面藏了一個很有用的programming tip. https://vimeo.com/ntpuecon/review/433919347/d91fe283fc
    yeelin-chen
    @yeelin-chen
    #5.4.5 -1
    my_fun2<-function(vec){
      ans_vec<-c()
      each<-unlist(map(vec,~.x%%2==0))
      condition_vec<-ifelse(each,"even","ord")
      for(n in 1:length(condition_vec)){
        condition=condition_vec[[n]]
        switch(condition,
             "even"={ans=n/2},
             "ord"={ans=-(n+1)/2})
        ans_vec<-c(ans_vec,ans)}
      return(ans_vec)
      }
    
    my_fun2(c(-11,32,19,20))
    2 replies
    老師我想問這5.4.5 的第一題這樣寫是對的嗎?
    Martin老師
    @tpemartin
    同學剛問了一個問題,這裡是補充。

    關於require()的使用:

    require("lubridate") 
    require(lubridate)

    都用來檢查有沒有lubridate。

    check_package <- function(pkgName){
      if(!require(pkgName)){ ## (*)
        install.packages(pkgName)
      }
    }

    因此(*)行裡 required(pkgName)會被當成檢查有沒有安裝一個名叫"pkgNmae"套件。我們要改require()設定讓它只接受require("lubridate"), 而不接受require(lubridate);當遇到沒有雙引號時會當成物件而去call它的字串值。這個設定是require(..., character.only=T)

    以下是正確寫法:

    check_package <- function(pkgName){
      if(!require(pkgName, character.only = T)){ ## (*)
        install.packages(pkgName)
      }
    }