LoginSignup
1
3

More than 5 years have passed since last update.

【R】要素数の異なるリストを行列に変換する方法

Posted at

要素数がすべて同じリストオブジェクトであれば、関数do.callを用いれば行列に変換することができます(リストオブジェクトをLISTとします)。

do.call(rbind,LIST)

しかし、リストの要素数が異なる場合、これができません。その場合、リストの要素数をそろえる必要があります。例えば、

lapply(LIST, function(x){c(x,rep(NA, max(sapply(LIST,length)) - length(x)))})

のように、リスト内の要素数の最大値をとってきて、それに合わせるようにNAを補完し、要素数をそろえます。
これで、関数do.callを使用し、行列に変換できます。

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