トップ 履歴 一覧 Farm ソース 検索 ヘルプ PDF RSS ログイン

pivot_longer()

*disclaimer
603498

R

pivot_longer()


tidyverseのパッケージのセットに入っているtidyrというパッケージに含まれる

  • 変換したい列を colsで指定 cols=列名
  • 変換したくないものは!をつける cols=!列名
    • 複数ある場合は、c()でまとめて、その前に ! cols=!c(列名, 列名)

サンプルデータ

> head(ipsyn.25.id)
    ID ipsyn.25 N.25 V.25 Q.25 S.25
146  1       35   12   11    1   11
200  2       41   15   13    2   11
150  3       43   12   13    5   13
194  4       44   10   16    7   11
169  5       45   11   14    9   11
187  6       45   16   14    1   14

変換

> library(tidyverse)
> ip25.mlt0 <- pivot_longer(ipsyn.25.id, cols=!ID)
> head(ip25.mlt0, 10)
# A tibble: 10 x 3
      ID name     value
   <int> <chr>    <int>
 1     1 ipsyn.25    35
 2     1 N.25        12
 3     1 V.25        11
 4     1 Q.25         1
 5     1 S.25        11
 6     2 ipsyn.25    41
 7     2 N.25        15
 8     2 V.25        13
 9     2 Q.25         2
10     2 S.25        11


応用例

  • ipsyn.25のスコア順に並べてIDがつけてある
ggplot(ip25.mlt0, aes(x=ID, y=value, color=name)) +
  geom_point() +
  geom_smooth() +
  theme_bw()



 Reference