首页 > 解决方案 > 如何在R中将两列合并为一列?

问题描述

我有一张有 3 列的表

A      B     C
1      2     3
2      4     5
3      3     6

我一直在尝试将列合并为一列。

输出应该是

x
1
2
3
2
4
3
3
5
6

标签: r

解决方案


你可以使用unlist

data.frame(x = unlist(df), row.names = NULL)

#  x
#1 1
#2 2
#3 3
#4 2
#5 4
#6 3
#7 3
#8 5
#9 6

或转换为矩阵:

data.frame(x = c(as.matrix(df)), row.names = NULL) 

数据

df <- structure(list(A = 1:3, B = c(2L, 4L, 3L), C = c(3L, 5L, 6L)), 
class = "data.frame", row.names = c(NA, -3L))

推荐阅读