0
0

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 3 years have passed since last update.

describeByの出力の個別要素にアクセスする方法

Last updated at Posted at 2021-03-17

Rのpsychパッケージに入ってるdescribeBy関数は、指定したグループ分け変数に基づいてグループ分けしたうえで、それぞれの記述統計量を返してくれる便利な関数。

で、この関数の出力から個別の要素を取り出したいときのための方法。

とりあえず、目的変数になるのはQ1-1とQ1-2という変数。
グループ分け変数はfQ5-1で、中身は13個ほどにグループ分けされている。

  r<-describeBy(dataset[[c("Q1-1","Q1-2")]],dataset[["fQ5-1"]])

とりあえず、あるグループだけを取り出したいとき。以下は例として9番目を取り出す。
[[]]と二つ重ねるのがポイント。

> r[[9]]
     vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
Q1-1    1 26 3.35 1.13      3    3.32 1.48   2   5     3 0.29    -1.37 0.22
Q1-2    2 26 3.04 0.72      3    3.00 0.00   2   5     3 0.57     0.44 0.14

グループ名でも取り出せる。
ちなみに、数字として9を入れた場合と、グループ名として"9"をいれた場合とで結果が違うのは、グループ名の通りに結果が並んでいるとは限らないため。

> r[["9"]]
     vars  n mean   sd median trimmed  mad min max range  skew kurtosis  se
Q1-1    1 25 3.12 1.01      3    3.19 1.48   1   5     4 -0.46    -0.55 0.2
Q1-2    2 25 2.96 0.98      3    3.00 1.48   1   5     4 -0.18    -0.46 0.2

平均を取り出す。

> r[[9]]$mean
[1] 3.346154 3.038462

> r[[9]]["mean"]
     mean
Q1-1 3.35
Q1-2 3.04

> r[[9]][["mean"]]
[1] 3.346154 3.038462

さらに問の個別の平均を取り出す。

> r[[9]]$mean[1]
[1] 3.346154

> r[[9]][["mean"]][1]
[1] 3.346154

> r[[9]]["mean"][1] #これはうまくいかない。
     mean
Q1-1 3.35
Q1-2 3.04

ということで取り出せる

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?