R reshape a vector into multiple columns

Vahid Mirjalili picture Vahid Mirjalili · Jul 19, 2013 · Viewed 40.2k times · Source

Let's say I have a vector in R as follows:

d<-seq(1,100)

I want to reshape this vector into a 10x10 matrix, so that I will have this data instead:

[,1]  [,2]  [,3]  ..  [,10]   
  1      2    3   ..   10
  11    12   13   ..   20
  21    22   23   ..   30
  ..
  91    92   93    ..  100

I tried to use reshape function, but it didn't work. Can someone help please?

Answer

flodel picture flodel · Jul 19, 2013

You can do

dim(d) <- c(10, 10)
d <- t(d)

or

d <- matrix(d, nrow = 10, byrow = TRUE)