資料清洗 (tidyverse包 管線處理)

2021-10-05 12:02:38 字數 2427 閱讀 5064

用現代優雅的方式。以管道是、泛函是程式設計技術實現了資料科學的整個流程:資料匯入、資料清洗、資料操作、資料視覺化、資料建模、可重現與互動報告。

1、magrittr包引入了管道操作,能夠通過管道將資料從乙個函式傳給另乙個函式。從而用若干個函式構成的管線一次變換資料。

2、管道運算子%>%的含義:將左邊的運算結果,以輸入的方式傳給右邊函式。若干個函式通過管道連線起來叫管道。

x%

>%f

()%>%g

() #相當於g(f

(x))

例如:

library(magrittr)

library(tidyverse)

warpbreaks

warpbreaks%>%

group_by(wool,tension)%>%

summarise_at(vars(breaks),list(~mean(.),~median(.),~sd(.)))

>

library

(magrittr)

>

library

(tidyverse)

> warpbreaks #這個資料是r中自帶的資料

breaks wool tension126

al230

al354

al425

al570

al652

al751

al826

al967

al1018am

> warpbreaks%

>

% #用warpbreaks這個資料

+group_by

(wool,tension)

%>

% #根據wool,tension進行分組

+summarise_at

(vars

(breaks)

,list(~

mean(.

),~median(.

),~sd

(.))

) #計算連續變數breaks的均值、中位數、標準差

# a tibble:

6 x 5

# groups: wool [2]

wool tension mean median sd

1al44.6

5118.12a

m24218.663a

h24.6

2410.34b

l28.2

299.865b

m28.8

289.436b

h18.8

174.89

3、管道的優點:省略中間變數,增加可讀性。讀起來就是對元資料集進行一系列操作的過程。

(1)管道預設將輸出穿給下乙個函式的第乙個引數。

>

.<-c

(1,3

,5,7

,na)>

mean(.

,na.rm =

true

) #na.rm相當於將資料中的na移除(rm是remove)[1

]4>c(

1,3,

5,7,

na)%>

%mean(.

,na.rm =

true)[

1]4>c(

1,3,

5,7,

na)%>

%mean

(na.rm =

true

) # "."可以省略[1

]4

(2)輸出可以在右邊多次使用(需要借助".")

>c(

1,3,

5,7)

%>

%plot(.

,main=

paste(.

,collapse =

",")

) #plot代表畫圖函式,main是其引數代表標題,paste代表將"."中的資料相連線,collapse=

","表示用,鏈結

#第乙個"."可以省略,其餘的不能省略。

>c(

1,3,

5,7)

%>

%plot

(main=

paste(.

,collapse =

",")

)

(3)若輸出傳遞給不是第乙個引數,需要手動放置"."

iris%

>

%plot

(sepal.width~petal.width,data =

.)

學習不易,請留下你的痕跡!

資料清洗之資料清洗概述

從廣泛的意義上來講,資料是乙個寬泛的概念,包括但不限於 我們要了解資料清洗,就需理解資料的內涵和外延 常見的資料有 其中,比較重要比較常見的分析資料是 資料。這裡重點介紹一些關於 資料的內容。資料 資料物件由屬性 attributes 及其值 value 構成 資料的特徵 什麼是資料清洗 資料清洗是...

excel資料清洗 資料清洗excel

資料清洗與加工 目的 獲得具備準確性 完整性和一致性符合分析質量的資料。資料處理第一步 資料清洗 1 資料去重 方式1 刪除重複項功能。適用於有重複項出現的列,並且這樣的重複無意義,比如標識列。操作 資料 選項卡下的 刪除重複值 按鈕 方式2 排序刪除重複項。適用於需要人工判斷無用重複項的資料,即將...

資料清洗技術 Excel資料清洗

1 了解 excel 的基本功能和用途 2 掌握 excel 資料清洗的基本步驟 3 了解 excel 資料清洗的方法 4 掌握 excel 常用的資料分析函式 5 掌握 excel 資料清洗常用的函式 作業系統 windows xp 7 8 10 excel版本 2007 2019 jdk版本 1...