Michale
Michale

Reputation: 131

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

Upvotes: 0

Views: 97

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389235

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))

Upvotes: 1

Related Questions