ざっくり:
install.packages("tidyverse")
でインストール── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
✓ ggplot2 3.3.3 ✓ purrr 0.3.4
✓ tibble 3.1.0 ✓ dplyr 1.0.3
✓ 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()
読み込まれるパッケージ
setwd()
を書いているものもありますが、RStudioプロジェクトでは必要ありません。
パッケージを使わないやり方より
2つのデータ形式(例: カテゴリごとの購買金額(千円))
df <- tibble::tibble("country" = c("a", "b", "c"),
"1999" = c(0.7, 0.3, 1.0),
"2000" = c(1.0, 2.0, 4.8),
"2001" = c(2.0, 5.0, 7.0))
df
# A tibble: 3 x 4
country `1999` `2000` `2001`
<chr> <dbl> <dbl> <dbl>
1 a 0.7 1 2
2 b 0.3 2 5
3 c 1 4.8 7
# A tibble: 9 x 3
country year amount
<chr> <chr> <dbl>
1 a 1999 0.7
2 a 2000 1
3 a 2001 2
4 b 1999 0.3
5 b 2000 2
6 b 2001 5
7 c 1999 1
8 c 2000 4.8
9 c 2001 7
# A tibble: 3 x 4
country `1999` `2000` `2001`
<chr> <dbl> <dbl> <dbl>
1 a 0.7 1 2
2 b 0.3 2 5
3 c 1 4.8 7
[1] 6
結果 <- スタート地点
を書いて、やりたい処理をパイプでつないでいくCtrl
+ Shift
+ M
Cmd
+ Shift
+ M
git clone https://github.com/ymattu/sampledata_small
例
select(product, 1:3) # 列番号が連続している場合
select(product, ProductID:Price) # 列名でも連続していれば同様
select(product, -CreatedDate) # 特定の列を除く
select(product, -4) # 特定の列番号を除く
select(product, starts_with("p")) # "p"で始まる列のみを選択
select(product, starts_with("p"), ignore.case = TRUE) # 大文字小文字を無視
select(product, matches("^(Product|Price)")) # "Product"または"Price"で始まる列を選択
$
はお手軽だしよく使います。$
で 1 列だけ取り出す[1] "雑貨・日用品" "花・グリーン" "食品"
[4] "衣料品" "ヘルス&ビューティー" "家具・インテリア・家電"
たくさんあるけど例えば
ユーザー、年ごとに集計
inner_join()
left_join()
full_join()
anti_join()
Answer : C++を使っているから
例えばこんな感じ(a, b, c 3 つのデータ)
x1 x2
1 A 1
2 B 2
3 C 3
x1 x3
1 A TRUE
2 B FALSE
3 D TRUE
x1 x4
1 B 10
2 C 11
3 D 12
Answer : 少し応用的ですが、purrrパッケージを使うと簡単です。
purrr パッケージの参考資料→そろそろ手を出す purrr