トップ 差分 一覧 ソース 検索 ヘルプ PDF RSS ログイン

dplyr

*disclaimer
33257

R

dplyr

library(dplyr)

filter()

> ToothGrowth %>% filter(supp=="VC") %>% head()
   len supp dose
1  4.2   VC  0.5
2 11.5   VC  0.5
3  7.3   VC  0.5
4  5.8   VC  0.5
5  6.4   VC  0.5
6 10.0   VC  0.5
> ToothGrowth %>% filter(supp=="OJ") %>% head()
   len supp dose
1 15.2   OJ  0.5
2 21.5   OJ  0.5
3 17.6   OJ  0.5
4  9.7   OJ  0.5
5 14.5   OJ  0.5
6 10.0   OJ  0.5
> ToothGrowth %>% filter(supp!="OJ") %>% head()
   len supp dose
1  4.2   VC  0.5
2 11.5   VC  0.5
3  7.3   VC  0.5
4  5.8   VC  0.5
5  6.4   VC  0.5
6 10.0   VC  0.5
> ToothGrowth %>% filter(dose==1.0) %>% head()
   len supp dose
1 16.5   VC    1
2 16.5   VC    1
3 15.2   VC    1
4 17.3   VC    1
5 22.5   VC    1
6 17.3   VC    1
> ToothGrowth %>% filter(dose>1.0) %>% head()
   len supp dose
1 23.6   VC    2
2 18.5   VC    2
3 33.9   VC    2
4 25.5   VC    2
5 26.4   VC    2
6 32.5   VC    2
> ToothGrowth %>% filter(dose<1.0) %>% head()
   len supp dose
1  4.2   VC  0.5
2 11.5   VC  0.5
3  7.3   VC  0.5
4  5.8   VC  0.5
5  6.4   VC  0.5
6 10.0   VC  0.5



mutate()

  • mutate(付け足すカラム名 = 変換操作)
> head(ToothGrowth)
   len supp dose
1  4.2   VC  0.5
2 11.5   VC  0.5
3  7.3   VC  0.5
4  5.8   VC  0.5
5  6.4   VC  0.5
6 10.0   VC  0.5
> ToothGrowth %>% mutate(result = len * dose) %>% head()
   len supp dose result
1  4.2   VC  0.5   2.10
2 11.5   VC  0.5   5.75
3  7.3   VC  0.5   3.65
4  5.8   VC  0.5   2.90
5  6.4   VC  0.5   3.20
6 10.0   VC  0.5   5.00

top_n(上位何位まで, カラム名)

  • 順位を指定しても、タイがある場合は、該当するもの全部
  • 上位を出してくれるが、並べ替えはしてくれない(並べ替えは、下の arrange())
> dim(YNUJ4_19)
[1] 2760    4
> head(YNUJ4_19)
  SL      MHD      MDD       diff
2  5 1.750000 1.750000  0.0000000
3  7 1.666667 1.833333 -0.1666667
4  5 1.250000 2.250000 -1.0000000
5 11 2.400000 1.700000  0.7000000
6  9 2.375000 1.625000  0.7500000
9  4 2.000000 1.000000  1.0000000
> YNUJ4_19 %>% top_n(5, MHD)
  SL      MHD      MDD     diff
1 17 5.062500 1.562500 3.500000
2 15 5.285714 1.285714 4.000000
3 19 5.055556 2.000000 3.055556
4 13 5.083333 1.333333 3.750000
5 19 5.111111 1.611111 3.500000
> YNUJ4_19 %>% top_n(5, SL)
   SL      MHD      MDD       diff
1  19 2.111111 3.055556 -0.9444444
2  19 4.611111 2.888889  1.7222222
3  19 3.000000 2.388889  0.6111111
4  19 4.777778 1.611111  3.1666667
5  19 2.111111 2.833333 -0.7222222
6  19 4.000000 2.222222  1.7777778
7  19 2.722222 2.388889  0.3333333
8  19 3.055556 2.722222  0.3333333
9  19 3.500000 1.666667  1.8333333
10 19 4.388889 1.555556  2.8333333
11 19 2.833333 2.833333  0.0000000
12 19 2.277778 3.000000 -0.7222222
13 19 5.055556 2.000000  3.0555556
14 19 5.111111 1.611111  3.5000000
15 19 2.611111 2.888889 -0.2777778
16 19 3.111111 2.388889  0.7222222
17 19 3.222222 2.611111  0.6111111
18 19 3.222222 2.222222  1.0000000
19 19 3.611111 2.388889  1.2222222
  • 下位から選ぶ場合は、マイナスをつける
> YNUJ4_19 %>% top_n(-5, diff)
  SL      MHD      MDD      diff
1 12 1.727273 3.909091 -2.181818
2  9 1.375000 3.875000 -2.500000
3 10 1.222222 3.777778 -2.555556
4 10 1.333333 3.666667 -2.333333
5 12 1.181818 5.000000 -3.818182
> tail(YNUJ4_19 %>% arrange(desc(diff)), 5)
     SL      MHD      MDD      diff
2756 12 1.727273 3.909091 -2.181818
2757 10 1.333333 3.666667 -2.333333
2758  9 1.375000 3.875000 -2.500000
2759 10 1.222222 3.777778 -2.555556
2760 12 1.181818 5.000000 -3.818182

arrange()

  • 昇順
  • 降順は arrange(desc())
> YNUJ4_19 %>% top_n(5, MHD) %>% arrange(MHD)
  SL      MHD      MDD     diff
1 19 5.055556 2.000000 3.055556
2 17 5.062500 1.562500 3.500000
3 13 5.083333 1.333333 3.750000
4 19 5.111111 1.611111 3.500000
5 15 5.285714 1.285714 4.000000
> YNUJ4_19 %>% top_n(5, MHD) %>% arrange(desc(MHD))
  SL      MHD      MDD     diff
1 15 5.285714 1.285714 4.000000
2 19 5.111111 1.611111 3.500000
3 13 5.083333 1.333333 3.750000
4 17 5.062500 1.562500 3.500000
5 19 5.055556 2.000000 3.055556
> YNUJ4_19 %>% top_n(5, MHD) %>% arrange(desc(SL))
  SL      MHD      MDD     diff
1 19 5.055556 2.000000 3.055556
2 19 5.111111 1.611111 3.500000
3 17 5.062500 1.562500 3.500000
4 15 5.285714 1.285714 4.000000
5 13 5.083333 1.333333 3.750000

> YNUJ4_19 %>% arrange(desc(MHD)) %>% top_n(10, MHD)
   SL      MHD      MDD     diff
1  15 5.285714 1.285714 4.000000
2  19 5.111111 1.611111 3.500000
3  13 5.083333 1.333333 3.750000
4  17 5.062500 1.562500 3.500000
5  19 5.055556 2.000000 3.055556
6  13 5.000000 1.916667 3.083333
7  14 5.000000 1.384615 3.615385
8  15 4.928571 1.642857 3.285714
9  13 4.833333 1.250000 3.583333
10 13 4.833333 1.833333 3.000000