![](/img/trans.png)
[英]How to combine two datasets in R with same number of rows but different columns without any join condition?
[英]How to combine two columns of different datasets in R?
我在 R 中有 2 個數據框:A 和 B。A 包含一列(“X1”)和 138 個 obs。 B 包含一列(“術語”),其中包含 520 個 obs。 我想將兩者組合成一個只有一列的新數據框,因此將包含 138+520=658 個觀察值。
我的數據:
A:
X1
1 word1
2 word2
3 word3
4 word4
.
.
138 word138
乙:
term
1 word139
2 word140
3 word141
4 word142
.
.
520 word658
它們都是不同的詞(來自兩組)。 我想創建一個新的數據集(“C”),它看起來像:
X
1 word1
2 word2
3 word3
4 word4
.
.
139 word139
.
.
658 word658
在確保它們都具有相同的名稱后,您可以使用rbind
:
C <- rbind('setNames(A, 'X'), setNames(B, 'X'))
另一種方法是將兩者連接起來:
C <- data.frame(X = c(A$X1, B$term))
我們可以在將列名重命名為X
后使用bind_rows
library(dplyr)
colnames(A) <- "X"
colnames(B) <- "X"
bind_rows(A, B)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.