【R】行の平均や合計や標準偏差を求めて追加する(apply, rowwise)

こんてんつ

行の平均や合計や標準偏差を求めて、新たな列を追加する方法について紹介する。次の2つの方法を提示する。

  1. apply
  2. rowwise

データ

irisデータセットを用いる。

> iris
    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1            5.1         3.5          1.4         0.2     setosa
2            4.9         3.0          1.4         0.2     setosa
3            4.7         3.2          1.3         0.2     setosa
4            4.6         3.1          1.5         0.2     setosa
5            5.0         3.6          1.4         0.2     setosa
6            5.4         3.9          1.7         0.4     setosa
7            4.6         3.4          1.4         0.3     setosa
…

apply

平均を作成するコードを紹介する。Rに実装されているmeanを用いて平均を返し、mutateで列を追加する。ココの部分をsumに変えて合計値を返すこともできる。sdに変えて標準偏差を返すこともできる。

コード

iris %>%
  mutate(avg = apply(iris[,c(1,2,3)],1,mean))

iris %>%
  mutate(avg = apply(iris[,c(1:3)],1,mean))

iris %>%
  mutate(avg = apply(iris[,c("Sepal.Length","Sepal.Width","Petal.Length")],1,mean))

出力

    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species      avg
1            5.1         3.5          1.4         0.2     setosa 3.333333
2            4.9         3.0          1.4         0.2     setosa 3.100000
3            4.7         3.2          1.3         0.2     setosa 3.066667
4            4.6         3.1          1.5         0.2     setosa 3.066667
5            5.0         3.6          1.4         0.2     setosa 3.333333
6            5.4         3.9          1.7         0.4     setosa 3.666667
7            4.6         3.4          1.4         0.3     setosa 3.133333
…

avgという列が追加されているのが分かる。

解説

applyの中の第1引数では、irisデータセットの中から特定の行を次の方法で取得している。

# irisの1列目,2列目,3列目
iris[,c(1,2,3)]

# irisの1列目~3列目
iris[,c(1:3)]

# irisのSepal.Length列,Sepal.Width列,Petal.Length列
iris[,c("Sepal.Length","Sepal.Width","Petal.Length")]

上記の実行結果はいずれも次のようになる。

    Sepal.Length Sepal.Width Petal.Length
1            5.1         3.5          1.4
2            4.9         3.0          1.4
3            4.7         3.2          1.3
4            4.6         3.1          1.5
5            5.0         3.6          1.4
6            5.4         3.9          1.7
7            4.6         3.4          1.4
…

また、apply関数はapply(データセット, 1, 計算条件)の様な構造である。コレの第2引数の1は行に対してという意味を表す。2は列に対してという意味だそうだが、今回は深追いしない。

rowwise

データフレームに対して、rowwise()を適用することで行処理を可能にする。具体的には、行単位でグループを作るという処理が為されている。今回は、平均を作成するコードを紹介する。Rに実装されているmeanを用いて平均を返し、mutateで列を追加する。

コード

iris %>%
  rowwise() %>%
  mutate(avg = mean(c(Sepal.Length,Sepal.Width,Petal.Length)))

iris %>%
  rowwise() %>%
  mutate(avg = mean(c_across(Sepal.Length:Petal.Length)))

出力

# A tibble: 150 x 6
# Rowwise: 
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species   avg
          <dbl>       <dbl>        <dbl>       <dbl> <fct>   <dbl>
 1          5.1         3.5          1.4         0.2 setosa   3.33
 2          4.9         3            1.4         0.2 setosa   3.1 
 3          4.7         3.2          1.3         0.2 setosa   3.07
 4          4.6         3.1          1.5         0.2 setosa   3.07
 5          5           3.6          1.4         0.2 setosa   3.33
 6          5.4         3.9          1.7         0.4 setosa   3.67
 7          4.6         3.4          1.4         0.3 setosa   3.13