前田和寛(@kazutan)
2015/6/13
プライヤのようにデータを操作
縦型データと横型データを変換
条件指定した行を抽出
指定した列を抽出
新しく列を追加、列の更新
並び替え
集約(集計)する
パッケージインストールと読み込み
install.packages("dplyr")
library("dplyr")
みんなのアイドルirisたんを使って説明します
df <- iris
df.virginica <- dplyr::filter(df, Species=='virginica')
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 6.3 3.3 6.0 2.5 virginica
2 5.8 2.7 5.1 1.9 virginica
3 7.1 3.0 5.9 2.1 virginica
dplyr::は「dplyrパッケージ内の」という意味ですdf.popo <- dplyr::select(df, c(Sepal.Width,Species))
Sepal.Width Species
1 3.5 setosa
2 3.0 setosa
3 3.2 setosa
subset()の時と同様新しい列を追加
df.pp <- dplyr::mutate(df.popo, pp=Sepal.Width*2)
Sepal.Width Species pp
1 3.5 setosa 7.0
2 3.0 setosa 6.0
3 3.2 setosa 6.4
列名がすでにある場合は更新
df.pp2 <- dplyr::mutate(df.pp, pp=Sepal.Width*3)
Sepal.Width Species pp
1 3.5 setosa 10.5
2 3.0 setosa 9.0
3 3.2 setosa 9.6
基本は昇順
df.pp3 <- dplyr::arrange(df.pp2, Sepal.Width)
Sepal.Width Species pp
1 2.0 versicolor 6.0
2 2.2 versicolor 6.6
3 2.2 versicolor 6.6
desc()と組み合わせると降順
df.pp3 <- dplyr::arrange(df.pp2, desc(Sepal.Width))
Sepal.Width Species pp
1 4.4 setosa 13.2
2 4.2 setosa 12.6
3 4.1 setosa 12.3
列の集計を実施。色々応用可能。
df.pp4 <- dplyr::summarize(df.pp3, pp.sum=sum(pp))
pp.sum
1 1375.8
df.pp5 <- dplyr::group_by(iris, Species)
df.pp6 <- dplyr::summarize(df.pp5, Sepal.Length.Mean = mean(Sepal.Length))
df.pp6
Source: local data frame [3 x 2]
Species Sepal.Length.Mean
1 setosa 5.006
2 versicolor 5.936
3 virginica 6.588
このコードが…
z <- dplyr::select(iris,c(Sepal.Width, Species))
zz <- dplyr::group_by(z, Species)
zzz <- dplyr::summarize(zz, Sepal.Width.Mean = mean(Sepal.Width))
zzz
こうなる!!
iris %>%
dplyr::select(.,c(Sepal.Width,Species)) %>%
dplyr::group_by(.,Species) %>%
dplyr::summarize(.,Sepal.Width.Mean = mean(Sepal.Width))
tidyr::gather関数iris2 <- dplyr::mutate(iris,id=rownames(iris))
z <- tidyr::gather(iris2, hoge, value, c(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width))
head(z,3)
Species id hoge value
1 setosa 1 Sepal.Length 5.1
2 setosa 2 Sepal.Length 4.9
3 setosa 3 Sepal.Length 4.7
tidyr::spread関数head(tidyr::spread(z,hoge,value),3)
Species id Sepal.Length Sepal.Width Petal.Length Petal.Width
1 setosa 1 5.1 3.5 1.4 0.2
2 setosa 10 4.9 3.1 1.5 0.1
3 setosa 11 5.4 3.7 1.5 0.2
他にも豊富な機能がはいってます
…詳しくは紹介した資料を見てください。
#ビールのみてぇ pic.twitter.com/ds5qLyXkXw
— kazutan (@kazutan) 2015, 2月 24