データはCOVID-19のワクチンの接種状況です。 次よりCSVファイルをダウンロードしました。
データを読み込みます。
# import a library
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.3 ✓ purrr 0.3.4
## ✓ tibble 3.0.4 ✓ dplyr 1.0.2
## ✓ tidyr 1.1.2 ✓ stringr 1.4.0
## ✓ readr 1.4.0 ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
# load the data
df0 <- read_csv("vaccination.csv")
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## Entity = col_character(),
## Code = col_character(),
## Date = col_date(format = ""),
## new_vaccinations_smoothed_per_million = col_double()
## )
列のデータのタイプを取得するコードは次です。
# apply the class() function to each column
df0 %>% map_chr(class)
## Entity Code
## "character" "character"
## Date new_vaccinations_smoothed_per_million
## "Date" "numeric"
列の重複をのぞいた数を取得するコードは次です。Entity列は国名なので、接種0の国のデータがなければ、90か国の接種がはじまっています。
# apply the n_distinct() function to each column
df0 %>% map_dbl(n_distinct)
## Entity Code
## 90 86
## Date new_vaccinations_smoothed_per_million
## 63 1784
データ・フレームを作成するコードは次です。
df0 %>% map_df(function(.x) {
return(data.frame(n_distinct = n_distinct(.x),
class = class(.x)))},
.id = "veriable")
## veriable n_distinct class
## 1 Entity 90 character
## 2 Code 86 character
## 3 Date 63 Date
## 4 new_vaccinations_smoothed_per_million 1784 numeric
~によって簡略なコードにすることもできます。
# create as data frame with the tilde-dot shorthand
df0 %>% map_df(~(data.frame(n_distinct = n_distinct(.x),
class = class(.x))),
.id = "variable")
## variable n_distinct class
## 1 Entity 90 character
## 2 Code 86 character
## 3 Date 63 Date
## 4 new_vaccinations_smoothed_per_million 1784 numeric
両者の対応を次に示します。