typeof(demand_return)
"closure"
typeof(demand_return)
"closure"
qs=0+2pqs=0+2p
q_s <- function(p){
q_s=0+2*p
return(q_s)
}
q_s
.
.
.
.
.
.
.
.
.
.
functional
函數的input可以是函數
while(continueCondition){ body }
留在迴圈的條件
if(sum==5){ break }
跳出迴圈的條件
"
只有這格要雙引號
1:4
sequence
{type}
class名稱
unique(courseStructure$科目名稱[pick])
uniqe外面也可以多一層return,但可省略
acadYear_i, grade_i, courseStructure
input argument
pick <- (courseStructure$學年==acadYear_i & str_detect(courseStructure$應修系級, grade_i)) return( unique(courseStructure$科目名稱[pick])
SOP一定寫在body裡面
pick <- (courseStructure$學年==acadYear_i & str_detect(courseStructure$應修系級, grade_i)) unique(courseStructure$科目名稱[pick])
這串是一直重複的,函數的SOP
internal dispatch
較建議此法!
myAge <- 40
此例兩邊效率一樣
&&
兩個&:效率較高 會從前面開始做,才做下一關
-1
也可寫成負無窮
100
也可寫成正無窮
if(!exists("weather_next36hours")){ jsonlite::fromJSON("https://opendata.cwb.gov.tw/fileapi/v1/opendataapi/F-C0032-001?Authorization=rdec-key-123-45678-011121314&format=JSON") -> weather_next36hours SOAR::Store(weather_next36hours) # 存在.R_cache environment中 }
askSilly_weather2多了這段
先設了這個條件:如果我要的"weather_next36hours"物件不存在,才進行下面動作,但進行完後先暫存在使用者看不到的地方(Store())
if(!require("stringr")){ install.packages("stringr") }
確認「是否沒有」安裝stringr,若TRUE則執行{}內的安裝程式
!
若沒加!:require()跑出TRUE表示「有安裝」 ## 加了!(negate) :TRUE的話代表使用者「沒有」安裝lubridate
# 先執行此行輸入學號 readline(prompt = "請輸入你的學號") -> studentId
此行要先單獨執行,整個才有效
warning("你所輸入的學號不正確")
警訊要用warning(),一般訊息用print()即可
intercept=10, slope=-5
相當於保有預設值,需要動的時候再去更改他的設定。
function本身的運作並不能只靠它的定義值function(...){...},還要有適當的環境(environment)條件才能生存。若物件的生存運作需要有適當的環境,表示電腦要存這個物件時還需要包含(enclose)它的(生存)環境一起存。這種儲存型態叫closure(閉包)。
電腦要儲存函數本身的值的時候,除了要存他這一連串的定義值以外,他還必須連他能夠存活的環境都要存下來。這種儲存物件值的同時要包含他的生存環境一起存的特殊資料型態,他的type就是closure
(1.2)
若沒打出(1.2),執行出來的只會是這個物件的定義值,要整個function運作,要加(1.2)
body
只是要查SOP的部分,使用body函數得到執行主體的步驟
,today()
拿掉
,today()
拿掉
,dateInput
可拿掉,因在這裡不會用到
# 找出符合地點的天氣資訊:weatherAtLocation (weather_next36hours$cwbopendata$dataset$location$locationName == locationInput) -> pick_location # 可挑出locationInput的「要/不要」向量 weather_next36hours$cwbopendata$dataset$location$weatherElement[pick_location][[1]] -> weatherAtLocation # 在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}度。")
這一整串就是SOP,整串放到function的大括號裡就可以了
middleLevels <- rep(paste0(start, "-", end), each = 2) # 每個新間距要2個
levels(原始factor) <- ... 中 ...的元素個數要和 左邊levels元素個數一樣多,而5歲變10歲,表示有2個舊間距要用同樣一個新間距。
str_detect(Ids, "[AC]") # 偵測 出現A、C
class logical 有A或C的 -> TRUE
str_detect
函數名稱
stringr
套件名稱
== 字串內容一模一樣。 str_detect 字串內容有關鍵字。
==要「值」和「位置」都相等 !!!!
誰
最後要的是名字,最後用example$name取
is.na(x2)
x2的每一項是不是NA: class logical FALSE TRUE FALSE
x %in% y
class logical
%in%: 屬於
class logical
paste0(c("他叫", "我叫"), c("小明", "小美", "大雄")) 會出現什麼?
他叫小明我叫小美他叫大雄
]
只要一個[ ]
a[-c(1, 3)]
沒有回存就只有該行會出現
list(
裡面會再新增一個Mary的list
以下選法行得通嗎? weather[[1]][[2]]$highest_temp # 今天/新北市/highest_temp weather[[2]][[2]]$highest_temp # 明天/新北市/highest_temp 如果不行,請把weather修改成可以那樣選。
today <- list(list("台北市",highest_temp = 25, lowest_temp = 18), list("新北市", highest_temp = 24, lowest_temp = 15)) tomorrow <- list(list("台北市", highest_temp = 25, lowest_temp = 18), list("新北市", highest_temp = 24, lowest_temp = 15))
weather <- list(today, tomorrow)
print(weather)
library(lubridate)
要用到ymd_hms
寫下了update
char
hms
不能改順序
長得像“2020-03-11T06:56:17Z”,它來自UTC時區
有T跟Z
hms
順序不能變
as.integer(majors10_factor)
factor再轉成integer,不同類有不同編號 經濟學系是1 社會學系是2
factor( c('經濟學系','社會學系','社會學系','經濟學系','經濟學系','社會學系','經濟學系','經濟學系','經濟學系','社會學系') )
character 先存成factor
majors10_factor
列出不同的名稱
typeof(majors10_factor)
只有幾類不同的,整數
int
整數 integer
sum(logi1)
要計算才會轉成1、0
當成1
但仍顯示TRUE
'
執行出的對話中要是',所以外面改成"
()
裡面放"."代替上一行的結果
Logical
T或F
c()
空向量,print(c(vNum3, vNum4))結果不會空格,會直接跳過該數字
~代表要形成一個公式(formula)
通常在計量模型會用到
依數據重排類別小技巧,善用named vector:
由小排到大或由大排到小
$
只能出現在結尾
Value A logical vector.
知道到時候得到的是TRUE或FALSE這種向量
coercible to one
如果你放的不是字串向量,然後r有辦法把強迫as.character的話,那也可以,但通常不建議強制轉換,因為可能出現意想不到的東西
negate = FALSE
Arguments裡有等號是default的意思,所以此negate = FALSE整段可以不要打,打到pattern就可!
Equivalent to
相當於
NaN
只會出現在數值向量,不會出現在字串向量等其他class
NA
不論是字串資料或是數字資料,NA都不用加雙引號!
not
要建立一個「不是」Taipeimetro的話:用!=來反轉
==: 等於
兩個等號才是「判斷」用! 一個等號用來命名