问题描述
我有两个数据框.
第一个只有一列和 10 行.
第二个是 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) 中的错误:
谁能建议另一个功能来做到这一点?
P.S 我也用列表尝试过这个,但它给出了同样的错误.
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功能.前 3 列有 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
这篇关于组合两个不同长度的数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!