Qiita Teams that are logged in
You are not logged in to any team

Community
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@Quantas

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

More than 1 year has passed since last update.

# 四分位数

## 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 <- 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
```
0
Help us understand the problem. What is going on with this article?
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