この記事ではR version 3.6.3、tidyverseパッケージを使っています。
コード
library(tidyverse)
iris %>%
pivot_longer(
cols = c( ###列の選択
"Sepal.Length",
"Sepal.Width",
"Petal.Length",
"Petal.Width"
),
names_to = "traits", #列名を格納する列の名前
values_to = "value" #値を格納する列の名前
)
結果
Species | traits | value |
---|---|---|
setosa | Sepal.Length | 5.1 |
setosa | Sepal.Width | 3.5 |
setosa | Petal.Length | 1.4 |
setosa | Petal.Width | 0.2 |
setosa | Sepal.Length | 4.9 |
setosa | Sepal.Width | 3.0 |
setosa | Petal.Length | 1.4 |
補足
かなりいろいろできそう。今後も更新予定