组合不同长度的两个数据帧 [英] combining two data frames of different lengths

查看:80
本文介绍了组合不同长度的两个数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据帧。

第一个只有一列和十行。

第二个是3列和50行。

I have two data frames.
The first is of only one column and 10 rows.
The second is of 3 columns and 50 rows.

当我尝试通过使用 cbind 来组合它时,会出现以下错误:

When I try to combine this by using cbind, it gives this error:

data.frame(...,check.names = FALSE)中的错误:

Error in data.frame(..., check.names = FALSE) :

可以任何人都建议另一个功能来做这个?

PS我也尝试过这个列表,但它给出了相同的错误。

Can anyone suggest another function to do this?
P.S I have tried this using lists too, but it gives the same error.

3列应该是CSV文件中的前3列,而使用一列的数据框应该是该文件中的第四列,当我使用 write.table写入功能。前三列有50行,第四列应该占据前10行。

The data frame consisting of 3 columns should be the first 3 columns in a CSV file, whereas the data frame with one column should be the fourth column in that file, when I write with the write.table function. The first 3 columns have 50 rows and the fourth column should occupy the first 10 rows.

推荐答案

plyr 包有一个函数 rbind.fill 将合并data.frames并引入 NA 为空单元格

In the plyr package there is a function rbind.fill that will merge data.frames and introduce NA for empty cells:

library(plyr)
combined <- rbind.fill(mtcars[c("mpg", "wt")], mtcars[c("wt", "cyl")])
combined[25:40, ]

    mpg    wt cyl
25 19.2 3.845  NA
26 27.3 1.935  NA
27 26.0 2.140  NA
28 30.4 1.513  NA
29 15.8 3.170  NA
30 19.7 2.770  NA
31 15.0 3.570  NA
32 21.4 2.780  NA
33   NA 2.620   6
34   NA 2.875   6
35   NA 2.320   4

这篇关于组合不同长度的两个数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆