可能重复:
How to join data frames in R (inner, outer, left, right)?
Combine two data frames and remove duplicate columns
如何向数据框添加多个列?
我有一个由189行和35列组成的现有数据帧(dataframe.X
)。为此,在第35列之后,我想在一个单独的数据框(dataframe.Y
)中添加19列,也包含189行。
如果它只是一列(并且dataframe.Y
只有一列),我会这样做:
dataframe.X$NEW.COLUMN<-dataframe.Y
但这不适用于多个专栏。有没有一种简单的方法可以做到这一点,而不是手动完成所有操作?
答案 0 :(得分:2)
您需要cbind()
的数据框方法:
A <- B <- data.frame(matrix(runif(100), ncol = 10))
names(A) <- LETTERS[1:10]
names(B) <- LETTERS[11:20]
AB <- cbind(A, B)
> head(AB)
A B C D E F G
1 0.7712438 0.8812705 0.84882438 0.75724291 0.921585941 0.5164942 0.18571224
2 0.0126251 0.1615442 0.60085004 0.05875528 0.253768847 0.2113731 0.87980683
3 0.3084463 0.2632898 0.78474323 0.79381024 0.090883591 0.2370151 0.73951553
4 0.3569714 0.9129827 0.31129223 0.75632920 0.866286356 0.2824447 0.96211833
5 0.8217136 0.9586726 0.87056708 0.93756202 0.001114461 0.5061194 0.06171122
6 0.5402234 0.4114434 0.05434122 0.40361466 0.536001187 0.4158735 0.99494631
H I J K L M N
1 0.40434088 0.7099546 0.9936925 0.7712438 0.8812705 0.84882438 0.75724291
2 0.20439643 0.2077438 0.5143336 0.0126251 0.1615442 0.60085004 0.05875528
3 0.83729465 0.1859405 0.7801276 0.3084463 0.2632898 0.78474323 0.79381024
4 0.06617131 0.7731577 0.1581497 0.3569714 0.9129827 0.31129223 0.75632920
5 0.58580980 0.4463787 0.1484671 0.8217136 0.9586726 0.87056708 0.93756202
6 0.30236632 0.3962285 0.8330804 0.5402234 0.4114434 0.05434122 0.40361466
O P Q R S T
1 0.921585941 0.5164942 0.18571224 0.40434088 0.7099546 0.9936925
2 0.253768847 0.2113731 0.87980683 0.20439643 0.2077438 0.5143336
3 0.090883591 0.2370151 0.73951553 0.83729465 0.1859405 0.7801276
4 0.866286356 0.2824447 0.96211833 0.06617131 0.7731577 0.1581497
5 0.001114461 0.5061194 0.06171122 0.58580980 0.4463787 0.1484671
6 0.536001187 0.4158735 0.99494631 0.30236632 0.3962285 0.8330804