0

I would like to merge 14 columns into one column in R.

If column one has an entry (1,2,3), column two (4,5,6), column three (7,8,9), I would like to merge them into one column (1,2,3,4,5,6,7,8,9)

Cœur
  • 37,241
  • 25
  • 195
  • 267
RoM
  • 1

3 Answers3

1

We can use unlist

unlist(df, use.names=FALSE)
#[1] 1 2 3 4 5 6 7 8 9

data

df <- data.frame(V1=1:3,V2= 4:6, V3= 7:9)
akrun
  • 874,273
  • 37
  • 540
  • 662
1

You can also use dim to change the dimension of the matrix.

a <- matrix(1:9, nrow=3)

dim(a) <- c(9,1)

print(a)

Jensc
  • 11
  • 2
1

I think the melt function from the reshape library will be a lot more useful since you can keep the names of the variables. Look at this example:

rectangle = data.frame("x"=c(0.0057,0.0209,-0.0058,-0.0209),"y"=c(-0.029,0.028,0.0250,-0.0028))



   melt(rectangle)
      variable   value
1        x  0.0057
2        x  0.0209
3        x -0.0058
4        x -0.0209
5        y -0.0290
6        y  0.0280
7        y  0.0250
8        y -0.0028
Diego Aguado
  • 1,604
  • 18
  • 36