Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(9)
希望这对你有用!
您可以使用
library(qpcR)
来组合两个大小不等的矩阵。注意:- 所得矩阵的大小为矩阵 2。
Hope this will work for you!
You can use
library(qpcR)
for combining two matrix with unequal size.NOTE:- The resultant matrix will be of size of matrix2.
只是我的2分钱。此代码将两个矩阵或 data.frame 合并为一个。如果一种数据结构的行数较少,则缺失的行将添加 NA 值。
Just my 2 cents. This code combines two matrices or data.frames into one. If one data structure have lower number of rows then missing rows will be added with NA values.
我实际上并没有遇到错误。
我使用了字母,以防连接所有数字具有不同的功能(但事实并非如此)。您的“第一个数据框”实际上只是一个向量”,只是在第四列中重复了 5 次...
但是专家对这个问题的所有评论仍然相关:)
I don't actually get an error with this.
I used letters incase joining all numerics had different functionality (which it didn't). Your 'first data frame', which is actually just a vector', is just repeated 5 times in that 4th column...
But all the comments from the gurus to the question are still relevant :)
我想我已经想出了一个相当短的解决方案..希望它对某人有帮助。
I think I have come up with a quite shorter solution.. Hope it helps someone.
我有类似的问题,我匹配两个数据集的特定列中的条目,并且仅在匹配时才进行 cbind 。
对于两个数据集,data1 & data2,在比较两者的第一列后,我在 data1 中从 data2 添加一列。
i had similar problem, i matched the entries in a particular column of two data sets and cbind only if it matched.
For two data sets, data1 & data2, i am adding a column in data1 from data2 after comparing first column of both.
在
plyr
包中,有一个函数rbind.fill
,它将合并 data.frames 并为空单元引入NA
:In the
plyr
package there is a functionrbind.fill
that will merge data.frames and introduceNA
for empty cells:鉴于后续评论,我根本不清楚OP实际上在追求什么。他们可能实际上正在寻找一种将数据写入文件的方法。
但假设我们确实在寻找一种
cbind
不同长度的多个数据帧的方法。cbind
最终会调用data.frame
,其帮助文件显示:因此,在OP的实际示例中,不应该出现错误,因为R应该回收长度为50的较短向量。事实上,当我运行以下命令时:
我没有收到任何错误,并且较短的数据帧按预期回收。但是,当我运行此命令时:
我收到以下错误:
但 R 的奇妙之处在于,您几乎可以让它执行您想要的任何操作,即使您不应该这样做。例如,这是一个简单的函数,它将
cbind
长度不均匀的数据帧并自动用NA
填充较短的数据帧:可以这样使用:
我不保证该功能在所有情况下都有效;它仅作为示例。
编辑
如果主要目标是创建 csv 或文本文件,您只需更改函数即可使用
""
而不是NA
进行填充code> 然后执行如下操作:然后在
rs
上使用write.table
。It's not clear to me at all what the OP is actually after, given the follow-up comments. It's possible they are actually looking for a way to write the data to file.
But let's assume that we're really after a way to
cbind
multiple data frames of differing lengths.cbind
will eventually calldata.frame
, whose help files says:so in the OP's actual example, there shouldn't be an error, as R ought to recycle the shorter vectors to be of length 50. Indeed, when I run the following:
I get no errors and the shorter data frame is recycled as expected. However, when I run this:
I get the following error:
But the wonderful thing about R is that you can make it do almost anything you want, even if you shouldn't. For example, here's a simple function that will
cbind
data frames of uneven length and automatically pad the shorter ones withNA
s:which can be used like this:
I make no guarantees that this function works in all cases; it is meant as an example only.
EDIT
If the primary goal is to create a csv or text file, all you need to do it alter the function to pad using
""
rather thanNA
and then do something like this:and then use
write.table
onrs
.参考Andrie的回答,建议使用
plyr::rbind.fill()
:与
t()
结合使用,您将获得类似于cbind.fill()
(不是plyr
的一部分)的东西,它将使用以下命令构建您的数据框:考虑相同的案件编号。Refering to Andrie's answer, suggesting to use
plyr::rbind.fill()
:Combined with
t()
you have something likecbind.fill()
(which is not part ofplyr
) that will construct your data frame with consideration of identical case numbers.我的想法是获取所有 data.frame 的最大行数,然后根据需要将空矩阵附加到每个 data.frame 。该方法不需要额外的包,只使用base。代码如下:
My idea is to get max of rows count of all data.frames and next append empty matrix to every data.frame if need. This method doesn't require additional packages, only base is used. Code looks following: