简体   繁体   中英

How to combine two columns into one in R?

I have one table with 3 columns

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

I have been trying to combine columns into one column.

The output should be

x
1
2
3
2
4
3
3
5
6

You could use 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

Or convert to matrix:

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

data

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM