cbind 一个带有空数据框的数据框 - cbind.fill?
我想我正在寻找 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()
替换为 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 值,这不是我想要的。
I think I'm looking for an analog of rbind.fill
(in Hadley's plyr
package) for cbind
. I looked, but there is no cbind.fill
.
What I want to do is the following:
#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
Understandably, this produces an error:
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 0, 1
My current fix is to replace the line return_df <- data.frame()
with return_df <- data.frame(dummy=1)
and then the code works. I then just remove dummy from the return_df
at the end. After adding the dummy and running the above code, I get
dummy a b l m
1 1 1 2 hi there 44
I then just need to get rid of the dummy, e.g.:
> return_df[,2:ncol(return_df)]
a b l m
1 1 2 hi there 44
I'm sure I'm missing an easier way to do this.
edit: I guess I'm not looking for a cbind.fill because that would mean that an NA value would be created after the cbind, which is not what I want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我建议修改泰勒的答案。我的函数允许使用向量对 data.frames 和/或矩阵进行 cbind 操作,而不会像泰勒的解决方案中那样丢失列名
I suggest a modification of Tyler's answer. My function allows
cbind
-ing of data.frames and/or matrices with vectors without loosing column names as it happens in Tyler's solution我只是发现了一个技巧,当我们想要将列添加到空数据框中时,只需首先rbind它,然后再cbind它。
我不知道为什么,但这对我有用。
I just find a trick that when we want to add columns into an empty dataframe, just rbind it at first time, than cbind it later.
I don't know why, but it works for me.
我们可以添加 id 列,然后使用合并:
We could add id column then use merge:
我们可以使用
list
代替data.frame
,并在最后将其转换为data.frame
。例如:We can use a
list
instead ofdata.frame
and convert it to adata.frame
at the end. For instance:对于
cbind.fill
命名列表,其中名称重叠并且您希望按名称cbind
,Tyler的答案可以修改为以下内容:To
cbind.fill
named lists, where the names overlap and you wish tocbind
by names, Tyler's answer may be modified to the following:虽然我认为 Tyler 的解决方案是直接的,也是最好的,但我只是提供另一种方法,使用我们已有的 rbind.fill() 。
While, I think Tyler's solution is direct and the best here, I just provide the other way, using
rbind.fill()
that we already have.使用 rowr::cbind.fill
Using
rowr::cbind.fill
qpcR 包中的 cbind.na 可以做到这一点。
cbind.na from the qpcR package can do that.
当
a
和b
是数据框时,以下应该可以正常工作:或者另一种可能性:
When
a
andb
are data frames, following should work just fine:or another possibility:
这是一个 cbind 填充:
让我们尝试一下:
我想我从某个地方偷了这个。
从这里编辑偷窃:链接
Here's a cbind fill:
Let's try it:
I think I stole this from somewhere.
EDIT STOLE FROM HERE: LINK