When to write a function

# Define example vector x

x <- 1:10

# Define rng

rng <- range(x, na.rm = TRUE)

# Rewrite this snippet to refer to the elements of rng

(x - rng[1]) /

(rng[2] - rng[1])

# Define example vector x

x <- 1:10

# Use the function template to create the rescale01 function

rescale01 <- function(x) {

# bdy

rng<-range(x,na.rm=T)

(x - rng[1]) / (rng[2] - rng[1])

}

# Test your function, call rescale01 using the vector x as the argument

rescale01(x)

# Define example vectors x and y

x <- c( 1, 2, NA, 3, NA)

y <- c(NA, 3, NA, 3,? 4)

# Turn this snippet into a function: both_na()

both_na<-function(x,y){

sum(is.na(x) & is.na(y))

}

mean_ci <- function(level, x) {

se <- sd(x) / sqrt(length(x))

alpha <- 1 - level

mean(x) + se * qnorm(c(alpha / 2, 1 - alpha / 2))

}

# Alter the mean_ci function

mean_ci <- function(x, level = 0.95) {

if (length(x) == 0) {

warning("`x` was empty", call. = FALSE)

return(c(-Inf, Inf))

}

se <- sd(x) / sqrt(length(x))

alpha <- 1 - level

mean(x) + se * qnorm(c(alpha / 2, 1 - alpha / 2))

}

replace_missings <- function(x, replacement) {

is_miss <- is.na(x)

x[is_miss] <- replacement

# Rewrite to use message()

message(sum(is_miss),replacement)

x

}

# Check your new function by running on df$z

replace_missings(df$z,0)

# Define col_sd() function

col_sd<-function(df){

output<-numeric(length(df))

for(i in seq_along(df)){

output[i]<-sd(df[[i]])

}

output

}

All the map functions inpurrrtake a vector,.x, as the first argument, then return.fapplied to each element of.x. The type of object that is returned is determined by function suffix (the part after_):

map()returns a list or data frame

map_lgl()returns a logical vector

map_int()returns a integer vector

map_dbl()returns a double vector

map_chr()returns a character vector

# Find the mean of each column

map_dbl(planes, mean)

# Find the mean of each column, excluding missing values

map_dbl(planes, mean, na.rm = TRUE)

# Find the 5th percentile of each column, excluding missing values

map_dbl(planes, quantile, probs = c(0.05), na.rm = TRUE)

# Save the result from the previous exercise to the variable models

models<-map(cyl, ~ lm(mpg ~ wt, data = .))

# Use map and coef to get the coefficients for each model: coefs

coefs<-map(models,~coef(.))

# Use string shortcut to extract the wt coefficient

map(coefs,"wt")

purrralso includes a pipe operator: %>%. The pipe operator is another shortcut that saves typing, but also increases readability. The explanation of the pipe operator is quite simple: x %>% f(y) is another way of writing f(x, y).

# Define models (don't change)

models <- mtcars %>%

split(mtcars$cyl) %>%

map(~ lm(mpg ~ wt, data = .))

# Rewrite to be a single command using pipes

models %>%

map(summary) %>%

map_dbl("r.squared")

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末身腻,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子豌骏,更是在濱河造成了極大的恐慌躺枕,老刑警劉巖劲室,帶你破解...
    沈念sama閱讀 219,270評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡始锚,警方通過(guò)查閱死者的電腦和手機(jī)瘪阁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)撒遣,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人管跺,你說(shuō)我怎么就攤上這事义黎。” “怎么了豁跑?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,630評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵廉涕,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我艇拍,道長(zhǎng)狐蜕,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,906評(píng)論 1 295
  • 正文 為了忘掉前任淑倾,我火速辦了婚禮馏鹤,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘娇哆。我一直安慰自己湃累,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布碍讨。 她就那樣靜靜地躺著治力,像睡著了一般。 火紅的嫁衣襯著肌膚如雪勃黍。 梳的紋絲不亂的頭發(fā)上宵统,一...
    開(kāi)封第一講書(shū)人閱讀 51,718評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音覆获,去河邊找鬼马澈。 笑死,一個(gè)胖子當(dāng)著我的面吹牛弄息,可吹牛的內(nèi)容都是我干的痊班。 我是一名探鬼主播,決...
    沈念sama閱讀 40,442評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼摹量,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼涤伐!你這毒婦竟也來(lái)了馒胆?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,345評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤凝果,失蹤者是張志新(化名)和其女友劉穎祝迂,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體器净,經(jīng)...
    沈念sama閱讀 45,802評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡型雳,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評(píng)論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了掌动。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片四啰。...
    茶點(diǎn)故事閱讀 40,117評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖粗恢,靈堂內(nèi)的尸體忽然破棺而出柑晒,到底是詐尸還是另有隱情,我是刑警寧澤眷射,帶...
    沈念sama閱讀 35,810評(píng)論 5 346
  • 正文 年R本政府宣布匙赞,位于F島的核電站,受9級(jí)特大地震影響妖碉,放射性物質(zhì)發(fā)生泄漏涌庭。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評(píng)論 3 331
  • 文/蒙蒙 一欧宜、第九天 我趴在偏房一處隱蔽的房頂上張望坐榆。 院中可真熱鬧,春花似錦冗茸、人聲如沸席镀。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,011評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)豪诲。三九已至,卻和暖如春挂绰,著一層夾襖步出監(jiān)牢的瞬間屎篱,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,139評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工葵蒂, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留交播,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,377評(píng)論 3 373
  • 正文 我出身青樓践付,卻偏偏與公主長(zhǎng)得像秦士,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子荔仁,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 心流是人們?nèi)硇耐度肽呈碌囊环N心理狀態(tài)乏梁。如藝術(shù)家在創(chuàng)作時(shí)次洼。人們處于這種情境時(shí),往往不愿被打擾遇骑,即抗拒中斷卖毁。定...
    預(yù)備起閱讀 1,008評(píng)論 0 2
  • 昨天還跟我老媽說(shuō)自己情商高亥啦, 今天就覺(jué)得自己愚蠢得可笑。 我真的笑了练链,也確實(shí)哭了翔脱, 因?yàn)檫@樣的感覺(jué)也許才是傷心。 ...
    櫻桃貓膩閱讀 673評(píng)論 1 0
  • 【關(guān)鍵詞:急驚風(fēng) 火熱的年輕人 旅行 自信 勇敢 過(guò)猶不及 有勇無(wú)謀 愛(ài)冒險(xiǎn) 魯莽 傲慢】 【解釋】 權(quán)杖騎士是極...
    武于喬閱讀 1,157評(píng)論 0 0
  • 讀梁實(shí)秋先生《生活的藝術(shù)》媒鼓,按生活篇章些許記錄届吁。 談生老病死 生 先生認(rèn)生日,一生中只能有一次绿鸣。因?yàn)樯挥幸粭l之...
    谷音sp閱讀 237評(píng)論 0 1