install.packages("パッケージ名")
でインストール
install.packages("ggplot2")
ざっくり:
install.packages("tidyverse")
でインストール── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
✓ ggplot2 3.3.3 ✓ purrr 0.3.4
✓ tibble 3.1.0 ✓ dplyr 1.0.7
✓ tidyr 1.1.4 ✓ stringr 1.4.0
✓ readr 1.4.0 ✓ forcats 0.5.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
読み込まれるパッケージ
setwd()
を書いているものもありますが、RStudioプロジェクトでは必要ありません。
read.csv()
stringsAsFactors = TRUE
がデフォルトになっているので、stringsAsFactors = FALSE
をつけることを推奨します。readr::read_csv()
data.table::fread()
readr::read_csv()
よりも高速data.table = FALSE
read_***()
関数が一番オススメread.*** | read_*** | fread | |
---|---|---|---|
速さ(45MB) | 3秒 | 0.8 秒 | 0.6秒 |
区切り値の判定ミス | × | × | △ |
エンコーディング | ○ | ○ | △ |
Encoding=cp932
Encoding=UTF8
パッケージを使わないやり方より
[1] 6
結果 <- スタート地点
を書いて、やりたい処理をパイプでつないでいくCtrl
+ Shift
+ M
Cmd
+ Shift
+ M
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
dat_m <- tibble::tibble(user = c('A', 'B', 'C'),
category_1 = c(10, 15, 8),
category_2 = c(2, 4, 5),
subject_1 = c(4, 5, 6),
subject_2 = c(5, 6, 7))
dat_m
# A tibble: 3 x 5
user category_1 category_2 subject_1 subject_2
<chr> <dbl> <dbl> <dbl> <dbl>
1 A 10 2 4 5
2 B 15 4 5 6
3 C 8 5 6 7
dat_long <- dat_m %>%
pivot_longer(cols = -user,
names_to = c("group", "num"),
names_sep = "_")
dat_long
# A tibble: 12 x 4
user group num value
<chr> <chr> <chr> <dbl>
1 A category 1 10
2 A category 2 2
3 A subject 1 4
4 A subject 2 5
5 B category 1 15
6 B category 2 4
7 B subject 1 5
8 B subject 2 6
9 C category 1 8
10 C category 2 5
11 C subject 1 6
12 C subject 2 7
Tokyo.R #79 の応用セッション を参照。
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] "衣料品" "ヘルス&ビューティー" "家具・インテリア・家電"
たくさんあるけど例えば
ユーザー、年ごとに集計
Answer : C++を使っているから
enjoy!