2016年3月2日 星期三

data.frame 中的 row 及 column 操作, 使用 dplyr 套件


Row Operations

Filtering and slicing rows

slice()
filter()

Sorting rows

arrange()

Select distinct rows

distinct()

Column Operations

Column Selecting and rename

select()

Add new column

mutate()

Column-wise descriptive statistics

summarise()
注意 column name 的英文大小寫有別

 #對 iris 中的萼片長度計算平均值及變異數  
 data("iris")  
 summarise(iris, mean.SL=mean(Sepal.Length), sd.SL = sd(Sepal.Length))  

結果:


Group-wise + Column-wise descriptive statistics

group_by() + summarise()
使用 group_by() 會產生另外一個 data.frame, 其中會包含 grouping 的資訊. 
這個分群後的 data.frame 進到 summarise() 函數時, summarise()會看到 data.frame 中的分群資訊, 自動會去計算分群後 各 column 的統計量. 使用 str() 可以看到 data.frame 中的分群資訊。

範例:
 # 對 iris 以 species 欄位進行分群, 再計算每群的萼片長度計算平均值及變異數  
 iris.grouped <- group_by(iris, Species)  
 summarise(iris.grouped, count=n(), mean.SL=mean(Sepal.Length), sd.SL=sd(Sepal.Length))  

結果:

參考資料:

Jaynal Abedin and Kishor Kumar Das, Data Manipulation with R, 2nd, Packt Publising, 2015

沒有留言: