R之dplyr::select/mutate函數擴展


select函數

dplyr包select函數用的很多,不過我們一般也是通過正反選列名或數字來選擇列。
iris數據集
常見用法如:

select(iris,c(1,3))
select(iris,1,3) #同上
select(iris,1:3)
select(iris,-c(1,3))

select(iris, Sepal.Length, Sepal.Width)
select(iris, Sepal.Length:Species)  
select(iris, -Sepal.Length, -Sepal.Width)
select(iris, one=Sepal.Length)

實際應用中我們加上一些輔助函數會更加得心應手。

這些函數有:

select(iris, starts_with("Sepal"))
select(iris, starts_with("sepal")) #默認忽略大小寫
select(iris, starts_with("Sepal", ignore.case = F)) #區分大小寫
select(iris, -starts_with("Sepal")) #同樣可以反選
select(iris,name=starts_with("Sepal")) #重命名

select(iris, ends_with(“Length”)) # 選擇列名以Length結尾的的列
select(iris, contains(“Sep”)) # 選擇列名包含有Sep的列
select(iris, matches(“\.”)) #選擇列名正則匹配到有’點’的列
select(iris, num_range(“Sepal.Length”, 1:5)) #選擇列名為Sepal.Length1到Sepal.Length5的列
select(iris, one_of(“Sepal.Length”, “Sepal.Width”)) #選擇列名為Sepal.Length和Sepal.Width的列
select(iris, everything()) #用於選擇所有變量(列名),一般用於改變列名順序用

mutate

mutate(iris,new=Sepal.Length*Sepal.Width) #在數據框后新增一列
transmute(iris,new=Sepal.Length*Sepal.Width) #另存為一個只有新增列的數據框

Ref: http://www.bioinfo-scrounger.com/archives/405
https://www.cnblogs.com/wkslearner/p/5741591.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM