【R】Tidyverse Tips as useful expressions

ドキュメントにも記載のないちょっとしたTipsがあります。これらを備忘録的にまとめていきます。

1. add_count

group_by, summarize, ungroupを行うのと同じです。これは、便利!

library(tidyverse)
library(tidylo)
# install.packages("lubridate")
# devtools::install_github("thebioengineer/tidytuesdayR")
library(tidytuesdayR)

tuesdata <- tt_load(2020, week = 22)
cocktails <- tuesdata$cocktails
cocktails %>% 
  add_count(drink) %>% 
  filter(n >= 12) %>% 
  knitr::kable()
| row_id|drink            |date_modified       | id_drink|alcoholic |category         |drink_thumb                                                          |glass         |iba |video | ingredient_number|ingredient      |measure               |  n|
|------:|:----------------|:-------------------|--------:|:---------|:----------------|:--------------------------------------------------------------------|:-------------|:---|:-----|-----------------:|:---------------|:---------------------|--:|
|     88|Angelica Liqueur |2016-08-31 19:21:12 |    12794|Alcoholic |Homemade Liqueur |http://www.thecocktaildb.com/images/media/drink/yuurps1472667672.jpg |Collins Glass |NA  |NA    |                 1|Angelica root   |3 tblsp chopped       | 12|
|     88|Angelica Liqueur |2016-08-31 19:21:12 |    12794|Alcoholic |Homemade Liqueur |http://www.thecocktaildb.com/images/media/drink/yuurps1472667672.jpg |Collins Glass |NA  |NA    |                10|Water           |1/4 cup               | 12|
|     88|Angelica Liqueur |2016-08-31 19:21:12 |    12794|Alcoholic |Homemade Liqueur |http://www.thecocktaildb.com/images/media/drink/yuurps1472667672.jpg |Collins Glass |NA  |NA    |                11|Food coloring   |1 drop yellow         | 12|
|     88|Angelica Liqueur |2016-08-31 19:21:12 |    12794|Alcoholic |Homemade Liqueur |http://www.thecocktaildb.com/images/media/drink/yuurps1472667672.jpg |Collins Glass |NA  |NA    |                12|Food coloring   |1 drop green          | 12|
|     88|Angelica Liqueur |2016-08-31 19:21:12 |    12794|Alcoholic |Homemade Liqueur |http://www.thecocktaildb.com/images/media/drink/yuurps1472667672.jpg |Collins Glass |NA  |NA    |                 2|Almond          |1 tblsp chopped       | 12|
|     88|Angelica Liqueur |2016-08-31 19:21:12 |    12794|Alcoholic |Homemade Liqueur |http://www.thecocktaildb.com/images/media/drink/yuurps1472667672.jpg |Collins Glass |NA  |NA    |                 3|Allspice        |1 cracked             | 12|
|     88|Angelica Liqueur |2016-08-31 19:21:12 |    12794|Alcoholic |Homemade Liqueur |http://www.thecocktaildb.com/images/media/drink/yuurps1472667672.jpg |Collins Glass |NA  |NA    |                 4|Cinnamon        |1 one-inch            | 12|
|     88|Angelica Liqueur |2016-08-31 19:21:12 |    12794|Alcoholic |Homemade Liqueur |http://www.thecocktaildb.com/images/media/drink/yuurps1472667672.jpg |Collins Glass |NA  |NA    |                 5|Anise           |3-6 crushed           | 12|
|     88|Angelica Liqueur |2016-08-31 19:21:12 |    12794|Alcoholic |Homemade Liqueur |http://www.thecocktaildb.com/images/media/drink/yuurps1472667672.jpg |Collins Glass |NA  |NA    |                 6|Coriander       |1/8 tsp powdered      | 12|
|     88|Angelica Liqueur |2016-08-31 19:21:12 |    12794|Alcoholic |Homemade Liqueur |http://www.thecocktaildb.com/images/media/drink/yuurps1472667672.jpg |Collins Glass |NA  |NA    |                 7|Marjoram leaves |1 tblsp fresh chopped | 12|
|     88|Angelica Liqueur |2016-08-31 19:21:12 |    12794|Alcoholic |Homemade Liqueur |http://www.thecocktaildb.com/images/media/drink/yuurps1472667672.jpg |Collins Glass |NA  |NA    |                 8|Vodka           |1.5 cup               | 12|
|     88|Angelica Liqueur |2016-08-31 19:21:12 |    12794|Alcoholic |Homemade Liqueur |http://www.thecocktaildb.com/images/media/drink/yuurps1472667672.jpg |Collins Glass |NA  |NA    |                 9|Sugar           |1/2 cup granulated    | 12|

2. crossing

考えられる全ての変数の組合せを生成。expand.gridと近いけど、dataframeを返します。

crossing(
  a = 1:2,
  b = c("a", "b"),
  c = c("x", "y")
) %>% 
  knitr::kable()
|  a|b  |c  |
|--:|:--|:--|
|  1|a  |x  |
|  1|a  |y  |
|  1|b  |x  |
|  1|b  |y  |
|  2|a  |x  |
|  2|a  |y  |
|  2|b  |x  |
|  2|b  |y  |

3. Type convert

適切な型に変換してくれます。

library(tidyverse)

typical_data <- tribble(~bad_col,
                        "3_apple",
                        "4_banana")

typical_data %>% 
  separate(bad_col, into = c("n", "fruit"), sep = "_") %>% 
  type_convert()
Parsed with column specification:
cols(
  n = col_double(),
  fruit = col_character()
)
# A tibble: 2 x 2
      n fruit 
  <dbl> <chr> 
1     3 apple 
2     4 banana

参考

Tidyverse tricks

Add a Comment

メールアドレスが公開されることはありません。