rbind.fill
の(Hadley のplyr
パッケージで)の類似物を探していると思いますcbind
。見ましたが、ありませんcbind.fill
。
私がやりたいことは次のとおりです。
#set these just for this example
one_option <- TRUE
diff_option <- TRUE
return_df <- data.frame()
if (one_option) {
#do a bunch of calculations, produce a data.frame, for simplicity the following small_df
small_df <- data.frame(a=1, b=2)
return_df <- cbind(return_df,small_df)
}
if (diff_option) {
#do a bunch of calculations, produce a data.frame, for simplicity the following small2_df
small2_df <- data.frame(l="hi there", m=44)
return_df <- cbind(return_df,small2_df)
}
return_df
当然のことながら、これによりエラーが発生します。
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 0, 1
return_df <- data.frame()
私の現在の修正は、行を次のように置き換えることreturn_df <- data.frame(dummy=1)
です。これにより、コードが機能します。最後にダミーを削除しreturn_df
ます。ダミーを追加して上記のコードを実行すると、
dummy a b l m
1 1 1 2 hi there 44
次に、ダミーを取り除く必要があります。たとえば、次のようになります。
> return_df[,2:ncol(return_df)]
a b l m
1 1 2 hi there 44
これを行う簡単な方法が欠けていると確信しています。
編集: cbind.fill を探していないと思います。これは、cbind の後に NA 値が作成されることを意味するためです。これは、私が望むものではありません。