在一个数据框中,我有 2 列和一列进入第二个 df。
我想将三列中的所有行合并为一列。
像合并一样test <- merge(df1$col1,df1$col2,df2$col2)
不是正确的解决方案。
知道哪个是正确的吗?
我认为你需要rbindlist
从data.table()
包
所以首先我创建了一些数据作为例子:
# Three different vectors that will be converted to data frame with single column
a <- as.data.frame(c(1:3))
b <- as.data.frame(LETTERS[seq(1:4)])
c <- as.data.frame(1:10)
# Then I've used function rbindlist to row bind
rbindlist(list(a, b, c))
# And the output (one column with total number 17)
c(1:3)
1: 1
2: 2
3: 3
4: A
5: B
6: C
7: D
8: 1
9: 2
10: 3
11: 4
12: 5
13: 6
14: 7
15: 8
16: 9
17: 10
更新
rbindlist()
适用于列表、data.frames 和 data.tables,而不适用于原子类型列表。所以你需要像这样编辑你的代码rbindlist(list(df1["col1"],df1["col2"],df2["col2"]))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句