0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

dplyrメモ(VA-11 Hall-Aのカクテルのデータと一緒に)

Last updated at Posted at 2019-07-07

目的

VA-11 Hall-Aのカクテルのデータを入力したので、これでdplyrをやってみる。

Rplot.png

dd <- dd %>% mutate(Material = factor(Material,
 levels = c('Adelhyde','Bronson Ext','Pwd Delta','Flanergide','Karmotrine')))

p <- ggplot(dd)
p <- p + geom_col(aes(x = Material, y = Qty, fill = Material))
p <- p + geom_text(aes(x = Material, y = Qty, label = Qty), nudge_y = 0.6)
p <- p + facet_wrap(~str_c(Name,Opt))
p <- p + scale_y_continuous(minor_breaks = NULL,limits = c(0, 10), breaks = 0:10, expand = c(0,0))
p <- p + theme(axis.text.x = element_text(angle = 90, vjust = 0.4))
p <- p + scale_fill_manual(values = c(rgb(226,39,67, maxColorValue = 255),
                                      rgb(254,199,61, maxColorValue = 255),
                                      rgb(135,166,227, maxColorValue = 255),
                                      rgb(161,199,113, maxColorValue = 255),
                                      rgb(197,228,228, maxColorValue = 255)
))
p

cocktails.png

No,Name,Adelhyde,Bronson Ext,Pwd Delta,Flanergide,Karmotrine,Opt,Age,Ice,Blend
1,Bad Touch,,2,2,2,4,,,T,
2,Beer,1,2,1,2,4,,,,
3,Bleeding Jane,,1,3,3,,,,,T
4,Bloom Light,4,,1,2,3,,T,T,
5,Blue Fairy,4,,,1,,T,T,,
6,Brandtini,6,,3,,1,,T,,
7,Cobalt Velvet,2,,,3,5,,,T,
8,Crevice Spike,,,2,4,,T,,,T
9,Fluffy Dream,3,,3,,,T,T,,
10,Fringe Weaver,1,,,,9,,T,,
11,Frothy Water,1,1,1,1,,,T,,
12,Grizzly Temple,3,3,3,,1,,,,T
13,Gut Punch,,5,,1,,T,T,,
14,Marsblast,,6,1,4,2,,,,T
15,Mercury Blast,1,1,3,3,2,,,T,T
16,Moonblast,6,,1,1,2,,,T,T
17,Piano Man,2,3,5,5,3,,,T,
18,Piano Woman,5,5,2,3,3,,T,,
19,Piledriver,,3,,3,4,,,,
20,Sparkle Star,2,,1,,,T,T,,
21,Sugar Rush,2,,1,,,T,,,
22,Sunshine Cloud,2,2,,,,T,,T,T
23,Suplex,,4,,3,3,,,T,
24,Zen Star,4,4,4,4,4,,,T,

読み込みと表示

read_csv関数を使う。

> d <- read_csv('VA-11 Hall-A.csv')
> d
# A tibble: 24 x 11
      No Name          Adelhyde `Bronson Ext` `Pwd Delta` Flanergide Karmotrine Opt   Age   Ice   Blend
   <int> <chr>            <int>         <int>       <int>      <int>      <int> <lgl> <lgl> <lgl> <lgl>
 1     1 Bad Touch           NA             2           2          2          4 NA    NA    TRUE  NA   
 2     2 Beer                 1             2           1          2          4 NA    NA    NA    NA   
 3     3 Bleeding Jane       NA             1           3          3         NA NA    NA    NA    TRUE 
 4     4 Bloom Light          4            NA           1          2          3 NA    TRUE  TRUE  NA   
 5     5 Blue Fairy           4            NA          NA          1         NA TRUE  TRUE  NA    NA   
 6     6 Brandtini            6            NA           3         NA          1 NA    TRUE  NA    NA   
 7     7 Cobalt Velvet        2            NA          NA          3          5 NA    NA    TRUE  NA   
 8     8 Crevice Spike       NA            NA           2          4         NA TRUE  NA    NA    TRUE 
 9     9 Fluffy Dream         3            NA           3         NA         NA TRUE  TRUE  NA    NA   
10    10 Fringe Weaver        1            NA          NA         NA          9 NA    TRUE  NA    NA   
# ... with 14 more rows

とりあえずビール

filter関数。

> d %>% filter(Name == 'Beer')
# A tibble: 1 x 11
     No Name  Adelhyde `Bronson Ext` `Pwd Delta` Flanergide Karmotrine Opt   Age   Ice   Blend
  <int> <chr>    <int>         <int>       <int>      <int>      <int> <lgl> <lgl> <lgl> <lgl>
1     2 Beer         1             2           1          2          4 NA    NA    NA    NA   

なんとかブラスト

str_detect関数。blastとBlastが混在していたのでstr_to_upper関数も使った。

> d %>% filter(T == str_detect(str_to_upper(Name), 'BLAST'))
# A tibble: 3 x 11
     No Name          Adelhyde `Bronson Ext` `Pwd Delta` Flanergide Karmotrine Opt   Age   Ice   Blend
  <int> <chr>            <int>         <int>       <int>      <int>      <int> <lgl> <lgl> <lgl> <lgl>
1    14 Marsblast           NA             6           1          4          2 NA    NA    NA    TRUE 
2    15 Mercury Blast        1             1           3          3          2 NA    NA    TRUE  TRUE 
3    16 Moonblast            6            NA           1          1          2 NA    NA    TRUE  TRUE 

Adelhydeってよく使うの?

group_by関数を使ってAdelhydeの含有量ごとにまとめたが、それ以降でnを普通の列として取り扱うためにはungroup関数を使う必要がある。
入っていないカクテルは7種類。入っているものだと、2つ投入するカクテルが5種類で一番多い。

> d %>% group_by(Adelhyde) %>% count()
# A tibble: 7 x 2
# Groups:   Adelhyde [7]
  Adelhyde     n
     <int> <int>
1        1     4
2        2     5
3        3     2
4        4     3
5        5     1
6        6     2
7       NA     7
> d %>% group_by(Adelhyde) %>% count() %>% ungroup() %>%
+     filter(F == is.na(Adelhyde)) %>% filter(n == max(n))
# A tibble: 1 x 2
  Adelhyde     n
     <int> <int>
1        2     5

じゃあAdelhydeを2つ使っているカクテルを教えて。

n関数。上で個数として出てきたnは関数としても使える。Adelhydeを含有量ごとにまとめて、要素数が5となるものを抜き出している。

> d %>% group_by(Adelhyde) %>% filter(n() == 5)
# A tibble: 5 x 11
# Groups:   Adelhyde [1]
     No Name           Adelhyde `Bronson Ext` `Pwd Delta` Flanergide Karmotrine Opt   Age   Ice   Blend
  <int> <chr>             <int>         <int>       <int>      <int>      <int> <lgl> <lgl> <lgl> <lgl>
1     7 Cobalt Velvet         2            NA          NA          3          5 NA    NA    TRUE  NA   
2    17 Piano Man             2             3           5          5          3 NA    NA    TRUE  NA   
3    20 Sparkle Star          2            NA           1         NA         NA TRUE  TRUE  NA    NA   
4    21 Sugar Rush            2            NA           1         NA         NA TRUE  NA    NA    NA   
5    22 Sunshine Cloud        2             2          NA         NA         NA TRUE  NA    TRUE  TRUE 

確かに、以下のほうが簡単。

> d %>% filter(Adelhyde == 2)
# A tibble: 5 x 11
     No Name           Adelhyde `Bronson Ext` `Pwd Delta` Flanergide Karmotrine Opt   Age   Ice   Blend
  <int> <chr>             <int>         <int>       <int>      <int>      <int> <lgl> <lgl> <lgl> <lgl>
1     7 Cobalt Velvet         2            NA          NA          3          5 NA    NA    TRUE  NA   
2    17 Piano Man             2             3           5          5          3 NA    NA    TRUE  NA   
3    20 Sparkle Star          2            NA           1         NA         NA TRUE  TRUE  NA    NA   
4    21 Sugar Rush            2            NA           1         NA         NA TRUE  NA    NA    NA   
5    22 Sunshine Cloud        2             2          NA         NA         NA TRUE  NA    TRUE  TRUE 

同じ材料でできているカクテルってあるの?

データの重複を調べるときにn関数が活躍する。Sparkle StarとSuger Rush、材料同じなんだ。知らなかった。

> d %>% group_by(Adelhyde, `Bronson Ext`, `Pwd Delta`, Flanergide, Karmotrine) %>% filter(n() != 1)
# A tibble: 2 x 11
# Groups:   Adelhyde, Bronson Ext, Pwd Delta, Flanergide, Karmotrine [1]
     No Name         Adelhyde `Bronson Ext` `Pwd Delta` Flanergide Karmotrine Opt   Age   Ice   Blend
  <int> <chr>           <int>         <int>       <int>      <int>      <int> <lgl> <lgl> <lgl> <lgl>
1    20 Sparkle Star        2            NA           1         NA         NA TRUE  TRUE  NA    NA   
2    21 Sugar Rush          2            NA           1         NA         NA TRUE  NA    NA    NA   
dd <- d %>% select(-Age, -Ice, -Blend) %>% gather(Material, Qty, -No, -Name, -Opt)
dd <- dd %>% mutate(Opt := str_c(Opt)) %>% mutate(Opt = case_when(
    Opt == 'TRUE' ~ ', Opt K',
    T ~ ""
))
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?