LoginSignup
1
0

More than 3 years have passed since last update.

R tips データの集計でよく使う関数 apply と tapply

Last updated at Posted at 2020-11-19

記事の目的

この記事では、データの集計でよく使用する関数であるapplyとtapplyについて紹介します。
sapplyやlapplyなどもありますが、使用したことがないのでこの2つについて解説します。

目次

No. 目次 説明
1 使用するデータ オリジナルデータ
2 apply 行や列ごとに関数を適応
3 tapply 1次元でグループごとに関数を適応
4 apply + tapply 多次元でグループごとに関数を適応

1. 使用するデータ

生徒5人の数学と英語の点数を作成しました。

Math <- c(50, 30, 20, 80, 50)
English <- c(60, 70, 80, 60, 70)
Data <- data.frame(Math, English)

image.png

2. apply (行や列ごとに関数を適応)

#行ごとの合計
A1 <- apply(Data, 1, sum)
#列ごとの平均
A2 <- apply(Data, 2, mean)
#列ごとの平均との差
A3 <- apply(Data, 2, function(x) x-mean(x))
Data A1(行ごとの合計) A2(列ごとの平均) A3(列ごとの平均との差)
image.png image.png image.png image.png

3. tapply (1次元でグループごとに関数を適応)

クラスデータを与え、クラスごとに関数を適応します。

Class <- c(1,1,2,2,1)
#数学の平均点
B1 <- tapply(Data$Math, Class, mean)
#英語の平均点
B2 <- tapply(Data$English, Class, mean)
#数学の偏差平方和
B3 <- tapply(Data$Math, Class, function(x) sum((x-mean(x))^2))
Data B1(数学の平均点) B2(英語の平均点) B3(数学の偏差平方和)
image.png image.png image.png image.png

4. apply + tapply (多次元でグループごとに関数を適応)

apply(Data, 2, function(x) tapply(x, Class, mean))

image.png

◯SNS
・youtube
https://youtube.com/channel/UCFDyXEywtNhdtwqC3GAkYuA

・Twitter
https://twitter.com/Dken_ta

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