r語言中mpg資料 R語言常用的資料處理的包 1

2021-10-12 13:33:28 字數 4232 閱讀 4011

在r中有很多的內建函式,比如transform()、rbind()、cbind()等函式,這些函式我們可以直接使用,除此之外,還有常見的幾種包在處理資料的時候非常好用。

dplyr包

dplyr包是hadley wickham(ggplot2包的作者,被稱為『乙個改變r的人』)的傑作, dplyr可用於處理r內部或者外部的結構化資料,相較於plyr包,dplyr專注接受dataframe物件, 大幅提高了速度,並且提供了更穩健的資料庫介面。

一、篩選: filter系列

filter、filter_all、filter_at、filter_if

用法:filter(.data, 條件...)

這裡我們用r的內建資料集作為例子(mtcars)

filter(mtcars, cyl == 8)

filter(mtcars, cyl < 6)

#過濾出cyl < 6 並且 vs == 1的行

filter(mtcars, cyl < 6 & vs == 1)

filter(mtcars, cyl < 6, vs == 1)

#過濾出cyl < 6 或者 vs == 1的行

filter(mtcars, cyl < 6 | vs == 1)

#過濾出cyl 為4或6的行

filter(mtcars, cyl %in% c(4, 6))

基於filter函式的用法,擴充套件出了一些新的函式

1filter_all(mtcars, any_vars(. > 150)) #篩選任何變數>150的樣本

2filter_at(mtcars, vars(starts_with("d")), any_vars((. %% 2) == 0))

3#篩選變數以「d」結尾,並且變數 "%%2" 等於0

4filter_if(mtcars, ~ all(floor(.) == .), all_vars(. != 0))

5# 篩選變數向下取整 == 原變數數值, 並且這部分變數的數值!= 0 的樣本集

二、slice函式

用法:slice(.data, ...)

用處: slice() 函式通過行號選取資料

#選取第一行的資料

slice(mtcars, 1l)

filter(mtcars, row_number() == 1l)

#選取最後一行資料

slice(mtcars, n())

filter(mtcars, row_number() == n())

#選取第5行到最後一行所有資料

slice(mtcars, 5:n())

filter(mtcars, between(row_number(), 5, n()))

這個函式在實際中並不常用,但是在某些情況下就會非常好用。

三、排列: arrange

用法:arrange(.data, ...)

arrange()按給定的列名依次對行進行排序,預設是按照公升序排序,對列名加 desc() 可實現倒序排序。原資料集行名稱會被過濾掉。

#以cyl和disp聯合公升序排序 ,如果cyl相同,按照disp

arrange(mtcars, cyl, disp)

#以disp降序排序

arrange(mtcars, desc(disp))

四、選擇: select

用法:select(.data, ...)

#選取變數名字首包含petal的列

select(iris, starts_with("petal"))

#選取變數名字首不包含petal的列

select(iris, -starts_with("petal"))

#選取變數名字尾包含width的列

select(iris, ends_with("width"))

#選取變數名字尾不包含width的列

select(iris, -ends_with("width"))

#選取變數名中包含etal的列

select(iris, contains("etal"))

#選取變數名中不包含etal的列

select(iris, -contains("etal"))

#正規表示式匹配,返回變數名中包含t的列

select(iris, matches(".t."))

#正規表示式匹配,返回變數名中不包含t的列

select(iris, -matches(".t."))

#直接選取列

select(iris, petal.length, petal.width)

#返回除petal.length和petal.width之外的所有列

select(iris, -petal.length, -petal.width)

#使用冒號連線列名,選擇多個列

select(iris, sepal.length:petal.width)

#選擇字元向量中的列,select中不能直接使用字元向量篩選,需要使用one_of函式

vars

select(iris, one_of(vars))

#返回指定字元向量之外的列

select(iris, -one_of(vars))

#返回所有列,一般調整資料集中變數順序時使用

select(iris, everything())

#調整列順序,把species列放到最前面

select(iris, species, everything())

一般我們用select函式選擇乙個資料的幾列

df df

#選擇v4,v5,v6三列

select(df, v4:v6)

select(df, num_range("v", 4:6))

五、變形: mutate

用法:mutate(.data, ...)

mutate()函式對已有列進行資料運算並新增為新列,

#新增新列wt_kg和wt_t,在同一語句中可以使用剛新增的列

mutate(mtcars, wt_kg = wt * 453.592, wt_t = wt_kg / 1000)

#計算新列wt_kg和wt_t,返回物件中只包含新列

transmute(mtcars, wt_kg = wt * 453.592, wt_t = wt_kg / 1000)

六、分組: group

用法:group_by(.data, ..., add = false)

group_by()用於對資料集按照給定變數分組,返回分組後的資料集。對返回後的資料集使用以上介紹的函式時,會自動的對分組資料操作。

#使用變數cyl對mtcars分組,返回分組後資料集

by_cyl

#返回每個分組中最大disp所在的行

filter(by_cyl, disp == max(disp))

#返回每個分組中變數名包含d的列,始終返回分組列cyl

select(by_cyl, contains("d"))

#使用mpg對每個分組排序

arrange(by_cyl, mpg)

#對每個分組無重複的取2行記錄

sample_n(by_cyl, 2)

七、資料關聯:join

類似於merge的用法

#內連線,合併資料僅保留匹配的記錄

inner_join(x,y, by = null, copy = false, suffix = c(".x", ".y"), ...)

#左連線,向資料集x中加入匹配的資料集y記錄

left_join(x,y, by = null, copy = false, suffix = c(".x", ".y"), ...)

#右連線,向資料集y中加入匹配的資料集x記錄

right_join(x,y, by = null, copy = false, suffix = c(".x", ".y"), ...)

#全連線,合併資料保留所有記錄,所有行

full_join(x,y, by = null, copy = false, suffix = c(".x", ".y"), ...)

#返回能夠與y表匹配的x表所有記錄

semi_join(x,y, by = null, copy = false, ...)

#返回無法與y表匹配的x表的所有記錄

anti_join(x, y, by = null, copy = false, ...)

當然,dplyr不止這一種用法,希望各位道友一起補充,豐富我們的資料資料經驗和生信分析的工作。

R語言中的esttab命令 R語言常用命令集合

plot x,y null,type p xlim null,ylim null,log main null,sub null,xlab null,ylab null,ann par ann axes true,frame.plot axes,panel.first null,panel.last ...

R語言中的esttab命令 R語言常用命令集合

plot x,y null,type p xlim null,ylim null,log main null,sub null,xlab null,ylab null,ann par ann axes true,frame.plot axes,panel.first null,panel.last ...

R語言中的引號

aa this is an example.1 this is an example.bb this is an example.1 this is an example.identical aa,bb 1 true anne s home 1 anne s home anne s home 1 a...