Help us understand the problem. What is going on with this article?

Rで第一・第三四分位数を求めるテンプレート

tags: rBasicLearning

四分位数

1. 四分位数を求めるテンプレート

四分位数を求めるテンプレートは, 以下に作りました.
宜しければ, 使ってください!
1. データの大きさの剰余で奇数か偶数かの分岐をif文で作ります
2. 前半のデータをデータの大きさの整数商で分けます(整数商にするのは, データの大きさが奇数でも偶数でも変わらないようにするためです)
3. 前半・後半のデータを取り出します(後半のデータは前半のデータを取り除く形で表しています)
4. 前半・後半のデータの中で中央値を取ります

Q1Q3 = function(x) {
  n = length(x)
  if (n %% 2 == 0) {
    firstHalf <- n %/% 2
    firstHalfData <- x[1:firstHalf]

    latterHalfData <- x[-(1:firstHalf)]
  } else {
    firstHalf <- n %/% 2
    firstHalfData <- x[1:firstHalf]

    latterHalfData <- x[-(1:(firstHalf + 1))]
  }
  return(c(Q1 = median(firstHalfData), Q3 = median(latterHalfData)))
}

2. 具体例

使うデータ

今回は, 奇数の大きさのデータ(oddData)と
偶数の大きさのデータ(evenData)を使います

oddData <- c(62, 64, 65, 67, 67, 68, 70, 73, 77, 80, 82)
evenData <- c(18, 20, 21, 23, 23, 25, 27, 29, 30, 31, 32, 39)

データの具体例

oddData <- c(62, 64, 65, 67, 67, 68, 70, 73, 77, 80, 82)
Q1Q3(oddData)

evenData <- c(18, 20, 21, 23, 23, 25, 27, 29, 30, 31, 32, 39)
Q1Q3(evenData)

データの出力

> evenData <- c(18, 20, 21, 23, 23, 25, 27, 29, 30, 31, 32, 39)
> Q1Q3(evenData)
  Q1   Q3 
22.0 30.5 
Quantas
初心者向けの投稿を多めにしてます!
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした