Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
因子型を使いこなそう@fukuoka.R#09
Search
nonki1974
September 09, 2017
Technology
250
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
因子型を使いこなそう@fukuoka.R#09
fukuoka.R #09 (2017/09/09) の発表資料
nonki1974
September 09, 2017
More Decks by nonki1974
See All by nonki1974
GTFS with Tidytransit package
nonki1974
0
340
TokyoR#84_Rexams
nonki1974
0
240
都道府県別焼き鳥屋ランキングの作成
nonki1974
1
930
Introduction to R
nonki1974
0
400
Introduction to dplyr
nonki1974
0
570
Introduction to ggplot2
nonki1974
1
550
Analyzing PSB tracks with R
nonki1974
0
630
introduction to fukuoka.R @ Fukuoka.LT
nonki1974
0
81
所要時間のヒートマップを作成する
nonki1974
0
610
Other Decks in Technology
See All in Technology
PfEingのアプローチで働こう
rindrics
0
180
Bet AI Day 2026丨AIを「使う」から、AIが「働く」へ ― LayerXが進める「組織AI」の社会実装
layerx
PRO
3
2.6k
AI-DLCって実際どう? 〜聞きたいこと全部聞いてみる〜
news_it_enj
0
240
その指示、Bobにしっかり伝わってる?初心者でも即実践できる プロンプト/コンテキスト設計のコツ
muehara
1
170
スクラムで身についていた動き方を、XPで捉え直してみた
codmoninc
PRO
1
250
データエンジニアリングワークショップ:Auto LoaderとSparkで学ぶデータパイプライン構築
databricksjapan
PRO
0
190
Genie Code ワークショップ 基礎編 / Genie-Code-Workshop-fundamental
databricksjapan
PRO
0
360
AIで仕事のやり方を変える
matsu7874
1
880
Microsoft 365 Copilot chat -tekoälypalvelun tietosuojaongelmat
hponka
0
440
生成AIのテナント制御とシャドーMCP対策 | AIを"止めずに"、情報を守る
yukun
0
120
Genie Code ワークショップ 応用編 / Genie-Code-Workshop-advanced
databricksjapan
PRO
0
360
Sony-DroidKaigi2026
sony
1
320
Featured
See All Featured
Optimising Largest Contentful Paint
csswizardry
37
3.9k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
330
The untapped power of vector embeddings
frankvandijk
2
1.9k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
2.7k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.7k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
4.3k
Statistics for Hackers
jakevdp
799
230k
Mobile First: as difficult as doing things right
swwweet
225
10k
YesSQL, Process and Tooling at Scale
rocio
174
15k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
270
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.8k
Transcript
因子型を使いこなそう! @nonki1974 2017/09/09 fukuoka.R#09
データ型 データ構造 データ型 ベクトル 行列・配列 リスト データフレーム 整数型 実数型 論理型
文字列 因子型
データの種類(尺度水準) 名義尺度 順序尺度 間隔尺度 比例尺度 同じものには同じ値(記号) 異なるものには異なる値(記号) 名義尺度+順序関係 値の間隔(差)に意味がある 10℃
→ 30℃ 20℃上昇した! 温度が3倍になった! 間隔尺度+値の比に意味がある
変数の種類 質的変数 名義尺度 順序尺度 量的変数 間隔尺度 比例尺度
変数の種類 質的変数 名義尺度 順序尺度 量的変数 間隔尺度 比例尺度 因子型
とりあえず文字型 > bloodtype <- c("B","A","A","A","AB", + "B","AB","A","A","A","C") > bloodtype [1]
"B" "A" "A" "A" "AB" "B" "AB" [8] "A" "A" "A" "C" > table(bloodtype) bloodtype A AB B C 6 2 2 1
因子型に変換 | factor()関数 > bloodtype.f <- factor(bloodtype, + levels =
c("A","B","O","AB")) > bloodtype.f [1] B A A A AB B AB [8] A A A <NA> Levels: A B O AB > table(bloodtype.f) bloodtype.f A B O AB 6 2 0 2 > table(addNA(bloodtype.f)) A B O AB <NA> 6 2 0 2 1 カテゴリ(水準)を ベクトルで指定 カテゴリに一致しない データは<NA>に <NA>もカテゴリ として扱う
因子型の中身 整数型ベクトル 水準ベクトル > str(bloodtype.f) Factor w/ 4 levels "A","B","O","AB":
2 1 1 1 4 2 4 1 1 1 ... > # 整数型に変換 > as.numeric(bloodtype.f) [1] 2 1 1 1 4 2 4 1 1 1 NA > # 対応する水準ベクトルの文字に変換 > as.character(bloodtype.f) [1] "B" "A" "A" "A" "AB" "B" "AB" [8] "A" "A" "A" NA
データフレーム > pokemon <- read.csv("pokemon.csv") > head(pokemon$type1) [1] くさ みず
むし むし ノーマル [6] ノーマル 15 Levels: あく いわ エスパー かくとう ... むし 文字列を含んだ列は因子型として読み込まれる
文字型として読み込みたい | stringsAsFactors > pokemon2 <- read.csv("pokemon.csv", + stringsAsFactors =
FALSE) > head(pokemon2$type1) [1] "くさ" "みず" "むし" "むし" [5] "ノーマル" "ノーマル" 文字列処理をしたい場合とか。
集計と棒グラフ > table(pokemon$sex) オス なし メス 45 4 23 >
barplot(table(pokemon$sex)) オス なし メス 0 10 20 30 40
水準の順序を変更 > pokemon$sex <- factor(pokemon$sex, + levels = c("オス", "メス",
"なし")) > head(pokemon$sex) [1] オス オス オス メス オス オス Levels: オス メス なし > barplot(table(pokemon$sex)) オス メス なし 0 10 20 30 40
カテゴリごとの箱ひげ図 > boxplot(weight~type1, data=pokemon)
水準ごとの統計量で水準を並べ替え > pokemon$type1 <- reorder(pokemon$type1, + pokemon$weight, + function(x){-median(x)}) >
boxplot(weight~type1, data=pokemon) Type1ごとの weightの中央値の 降順に水準を並び替え
因子型の扱い 結構めんどくさい
そこで
forcats package > # パッケージのインストール > install.packages("forcats") > # パッケージのロード
> library(forcats) > pokemon <- read.csv("pokemon.csv") > boxplot(weight~fct_reorder(type1,weight,median,.desc=T), + data=pokemon)
例えば fct_lump() > # 頻出4水準のみ残して、後はOtherにする > boxplot(weight~fct_lump(pokemon$type1, 4), data=pokemon)
詳細は forcats kazutan
enjoy!