RoM
RoM

Reputation: 1

Combining multiple columns in R

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)

Upvotes: 0

Views: 254

Answers (3)

Diego Aguado
Diego Aguado

Reputation: 1606

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

Upvotes: 1

Jensc
Jensc

Reputation: 11

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)

Upvotes: 1

akrun
akrun

Reputation: 887951

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)

Upvotes: 1

Related Questions