【发布时间】:2011-12-19 05:19:30
【问题描述】:
我想我正在为cbind 寻找rbind.fill 的类似物(在Hadley 的plyr 包中)。我看了看,没有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(dummy=1) 替换行return_df <- data.frame(),然后代码就可以工作了。然后我在最后从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 值,这不是我想要的。
【问题讨论】:
-
没有数据集和预期的输出,很难确切知道您想要什么。
-
@TylerRinker,你是对的。我确实描述了我当前的修复,但我没有明确说明我想要的结果是什么。我现在已经添加了这些信息。