2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rで「来店した動機はなんですか?」のアンケート結果を分析

Last updated at Posted at 2017-01-12

はじめに

Rを使って「来店した動機はなんですか?」という内容のアンケート結果を分析する方法を記録したいと思います。
今回は「通りがかり」「知人の紹介」などの来店動機を、性別ごとに分析しようと思います。

対象データ

enquete.csv
sex,trigger
"男性",""
"女性","通りすがり"
"男性","通りすがり"
"女性","知人の紹介"
"男性","通りすがり"
"女性","通りすがり"
"女性","知人の紹介"
-------

左から→性別,きっかけ
となります。
こんな感じのcsvデータを利用します。

結果グラフ

graph.png

pie_m.png

pie_f.png

コード

# ggplotのインストール
install.packages("ggplot2")

# dplyrのインストール(データフレーム操作を支援するライブラリ)
install.packages("dplyr")

# モジュールのロード
library(ggplot2)
library(dplyr)

# csvの読み込み(先頭行をヘッダに。ファイル文字コードはUTF-8。セパレータは,)
# パラメータをきちんと明示した方が安定する
# encoding="UTF-8"とfileEncoding="UTF-8"は違うので注意
df <- read.csv("enquete.csv", header = TRUE, fileEncoding="UTF-8", stringsAsFactors = FALSE, sep = ",")

# 先頭行を出力
head(df)
##     sex    trigger
##  1 男性
##  2 女性 通りすがり
##  3 男性 通りすがり
##  4 女性 知人の紹介
##  5 男性 通りすがり
##  6 女性 通りすがり

# 性別ときっかけどちらかが空白のデータは除外
df <- df[df$sex != "", ]
df <- df[df$trigger != "", ]

# 先頭行を出力
head(df)
##     sex    trigger
##  2 女性 通りすがり
##  3 男性 通りすがり
##  4 女性 知人の紹介
##  5 男性 通りすがり
##  6 女性 通りすがり
##  7 女性 知人の紹介

# 性別ときっかけそれぞれでgoroupする(dplyrの機能)
df <- group_by(df, trigger, sex)

# group対象の行数をカウントしてcountというカラムを追加(dplyrの機能)
df <- summarize(df, count=n())

# arrangeを使ってtriggerの降順で並び替え(dplyrの機能)
df <- arrange(df, desc(trigger))

# 先頭行を出力
head(df)

##  Source: local data frame [6 x 3]
##  Groups: trigger [3]
##  
##       trigger   sex count
##         <chr> <chr> <int>
##  1 通りすがり  女性   285
##  2 通りすがり  男性   180
##  3 知人の紹介  女性    96
##  4 知人の紹介  男性    36
##  5 ネット検索  女性   132
##  6 ネット検索  男性   105

# plotを作成(棒グラフ用)
gp <- ggplot(
  df,
  aes (
    x = trigger,
    y = count,
    fill= sex
  )
) 
gp <- gp + geom_bar(position = "dodge", stat = "identity") 
gp <- gp + ggtitle("来店動機の男女比")
plot(gp)

# plotを作成(男性円グラフ用)
df_m <- data.frame(
  trigger = df[df$sex=="男性","trigger"],
  count = df[df$sex=="男性","count"]
)
gp <- ggplot(df_m, aes(x="", y=count, fill=trigger))
gp <- gp + geom_bar(width = 1, stat = "identity")
gp <- gp + coord_polar(theta = "y")
gp <- gp + ggtitle("男性の動機")
gp

# plotを作成(女性円グラフ用)
df_f <- data.frame(
  trigger = df[df$sex=="女性","trigger"],
  count = df[df$sex=="女性","count"]
)
gp <- ggplot(df_f, aes(x="", y=count, fill=trigger))
gp <- gp + geom_bar(width = 1, stat = "identity")
gp <- gp + coord_polar(theta = "y")
gp <- gp + ggtitle("女性の動機")
gp

まとめ

今回は「来店した動機はなんですか?」のアンケート回答結果の分析をしてみました。
参考になりましたでしょうか。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?