LoginSignup
2
5

More than 5 years have passed since last update.

Rで回答結果を複数まとめて表にする

Posted at

アンケート調査の際、例えば5段階で回答してもらう設問が5つあるときに、それをまとめてレポートしたいことがある。
これを統計パッケージのRを使って、実現する。
tableを使うとクロス集計になってしまう。

好きな食べ物(それぞれ1−5で回答)
* ごはん
* パン
* ピザ
* ケーキ

こんな感じの設問をまとめて以下のような表にする感じ。

image

関数を作っておくと便利。

ma_sample.r
# 各選択肢に対する回答数と回答の平均値
ma_one <- function(x) c(table(x),mean(x))
# 全体を一つの表にまとめて、ラベルをつける

create_ma <- function(x,y,z) {
      ma_res <- t(sapply(x,ma_one))
      dimnames(ma_res) <- list(y,z)
      return(ma_res)      
      }
res <- create_ma(r, c("ごはん","パン","ピザ", "ケーキ"),c("1","2","3","4","5","平均"))

出力はxtableを使うことで、LaTeXのソースなどを出力できる。

マルチアンサーのケースでは、ma_oneの後に回答数の合計を入れておくとよい。

2
5
1

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
5