【R】データに条件を指定した列を追加する(mutate)

こんてんつ

データフレームに列を追加する方法を紹介する。条件によって何らかの値を計算したり、文字列を追加したりする。tidyverseパッケージ集の中のdplyrパッケージのmutate関数を利用します。

早見チートシート

#任意の文字列を追加する

iris_column_added <- iris %>%
  mutate(new_col = "AAA") %>%
  select(new_col,everything())

#条件によって追加する方法を変える

iris_column_added <- iris %>%
  mutate(new_col = if_else(Sepal.Length >= 5.0, "AAA", "BBB")) %>%
  select(new_col, everything())

iris_column_added <- iris %>%
  mutate(new_col = case_when(Sepal.Length >= 5.5 ~ "AAA",
                         Sepal.Length >= 5.0 & Sepal.Length < 5.5 ~ "BBB",
                         TRUE ~ "CCC")) %>%
  select(new_col, everything())

#計算式によって追加する

iris_column_added <- iris %>%
  mutate(new_col = Sepal.Length + Sepal.Width) %>%
  select(new_col, everything())

iris_column_added <- iris %>%
  mutate(new_col = if_else(Sepal.Length >= 5.0, Sepal.Length, 0)) %>%
  select(new_col, everything())

解説

mutateによってnew_colという新しい列を追加した後、select&everythingによって、new_col列を一番前に持ってきたデータフレームにしている。select&everythingを使わなければ、一番右に列が追加される。select&everythingの解説は以下の記事を参考。

contents-open.hatenablog.com

詳細

任意の文字列を追加する

iris_column_added <- iris %>%
  mutate(new_col = "AAA") %>%
  select(new_col,everything())
head(iris_column_added)
> head(iris_column_added)
  new_col Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1     AAA          5.1         3.5          1.4         0.2  setosa
2     AAA          4.9         3.0          1.4         0.2  setosa
3     AAA          4.7         3.2          1.3         0.2  setosa
4     AAA          4.6         3.1          1.5         0.2  setosa
5     AAA          5.0         3.6          1.4         0.2  setosa
6     AAA          5.4         3.9          1.7         0.4  setosa

条件によって追加する方法を変える

if_else関数によって、条件に合うものをAAAに、それ以外のものをBBBとして列を追加する。

iris_column_added <- iris %>%
  mutate(new_col = if_else(Sepal.Length >= 5.0, "AAA", "BBB")) %>%
  select(new_col, everything())
head(iris_column_added)
> head(iris_column_added)
  new_col Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1     AAA          5.1         3.5          1.4         0.2  setosa
2     BBB          4.9         3.0          1.4         0.2  setosa
3     BBB          4.7         3.2          1.3         0.2  setosa
4     BBB          4.6         3.1          1.5         0.2  setosa
5     AAA          5.0         3.6          1.4         0.2  setosa
6     AAA          5.4         3.9          1.7         0.4  setosa

case_when関数によって、一つ目の条件に合致するものをAAAとし、二つ目の条件に合致するものをBBBとし、それ以外のものをTRUEで繋いでCCCで出力するようにしている。条件と出力は~で繋ぐ。

iris_column_added <- iris %>%
  mutate(new_col = case_when(Sepal.Length >= 5.5 ~ "AAA",
                         Sepal.Length >= 5.0 & Sepal.Length < 5.5 ~ "BBB",
                         TRUE ~ "CCC")) %>%
  select(new_col, everything())
head(iris_column_added,20)
> head(iris_column_added,20)
   new_col Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1      BBB          5.1         3.5          1.4         0.2  setosa
2      CCC          4.9         3.0          1.4         0.2  setosa
3      CCC          4.7         3.2          1.3         0.2  setosa
4      CCC          4.6         3.1          1.5         0.2  setosa
5      BBB          5.0         3.6          1.4         0.2  setosa
6      BBB          5.4         3.9          1.7         0.4  setosa
7      CCC          4.6         3.4          1.4         0.3  setosa
8      BBB          5.0         3.4          1.5         0.2  setosa
9      CCC          4.4         2.9          1.4         0.2  setosa
10     CCC          4.9         3.1          1.5         0.1  setosa
11     BBB          5.4         3.7          1.5         0.2  setosa
12     CCC          4.8         3.4          1.6         0.2  setosa
13     CCC          4.8         3.0          1.4         0.1  setosa
14     CCC          4.3         3.0          1.1         0.1  setosa
15     AAA          5.8         4.0          1.2         0.2  setosa
16     AAA          5.7         4.4          1.5         0.4  setosa
17     BBB          5.4         3.9          1.3         0.4  setosa
18     BBB          5.1         3.5          1.4         0.3  setosa
19     AAA          5.7         3.8          1.7         0.3  setosa
20     BBB          5.1         3.8          1.5         0.3  setosa

計算式によって追加する

足し算で列を追加した。

iris_column_added <- iris %>%
  mutate(new_col = Sepal.Length + Sepal.Width) %>%
  select(new_col, everything())
head(iris_column_added)
> head(iris_column_added)
  new_col Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1     8.6          5.1         3.5          1.4         0.2  setosa
2     7.9          4.9         3.0          1.4         0.2  setosa
3     7.9          4.7         3.2          1.3         0.2  setosa
4     7.7          4.6         3.1          1.5         0.2  setosa
5     8.6          5.0         3.6          1.4         0.2  setosa
6     9.3          5.4         3.9          1.7         0.4  setosa

条件に合致するものは、同じ値を。合致しない場合は0となるような列を追加した。

iris_column_added <- iris %>%
  mutate(new_col = if_else(Sepal.Length >= 5.0, Sepal.Length, 0)) %>%
  select(new_col, everything())
head(iris_column_added)
> head(iris_column_added)
  new_col Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1     5.1          5.1         3.5          1.4         0.2  setosa
2     0.0          4.9         3.0          1.4         0.2  setosa
3     0.0          4.7         3.2          1.3         0.2  setosa
4     0.0          4.6         3.1          1.5         0.2  setosa
5     5.0          5.0         3.6          1.4         0.2  setosa
6     5.4          5.4         3.9          1.7         0.4  setosa

参考情報

行の合計、平均、標準偏差など高度な処理によって列を追加したい場合は、次の記事を参考にしてみて下さい。

contents-open.hatenablog.com