This code converts a vector into a matrix and then saves the matrix into your clipboard.

Before we do that, let’s go over the basics. First you need a vector. I will use a vector called x which will have the numbers 1 through 12 stored in it. Now we can convert this into any kind of matrix we want, a 6 by 2, 3 by 4, 4 by 3, or even a 12 by 1. Let’s do a 6 by 2 matrix and then a 3 by 4 matrix so we can see how the code changes.

x<- 1:12
x_matrix <- matrix(x,6,2)
x_matrix
##      [,1] [,2]
## [1,]    1    7
## [2,]    2    8
## [3,]    3    9
## [4,]    4   10
## [5,]    5   11
## [6,]    6   12
zero_matrix <-matrix(0,6,2) #fill with zeros
zero_matrix
##      [,1] [,2]
## [1,]    0    0
## [2,]    0    0
## [3,]    0    0
## [4,]    0    0
## [5,]    0    0
## [6,]    0    0
x_byrow_matrix <-matrix(x,6,2,byrow=T) #fill in by rows
x_byrow_matrix
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4
## [3,]    5    6
## [4,]    7    8
## [5,]    9   10
## [6,]   11   12
x_threefour_matrix<-matrix(x,3,4)
x_threefour_matrix
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
x_three_matrix <-matrix(x,3) # you don't need to specify columns!
x_three_matrix
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12

The first thing to notice is that the first thing you specify is what you want to be saved into the matrix, I used x, but you can fill the matrix with anything. Below it I create a 6 by 2 matrix filled with 0’s

The next thing to notice is that you always specify a matrices indices with rows first. Row by column, r by c, remember it! You will also notice looking at the x_matrix that the matrix command fills in by column, not rows. We can change the command to make it fill in by rows with the byrow method. Lastly, you can see that you don’t even need to specify how many columns you want in your data set because R can figure out it out if you specify the rows you want (It’s good practice to specify the number of columns though…)!

R makes this data transformation really easy.

I use this code for data entry because one of my data sources very inconveniently saves my data as a vector. I use R to convert this vector into a matrix and then I copy it to my clipboard so that I can paste it into an excel file.

data<- read.csv(file="/Users/Leland/Desktop/Fitbit Data/Rconvert.csv",head=F,sep=",")
vector_data<-data[,1]
head(vector_data)
## [1] 336 157 193  53 185 104
mymat<-matrix(vector_data, 12, byrow =F) #specify how many rows you need, in this case, I have 12!
#write.table(mymat, 'clipboard', sep='\t') uncomment to use this command
head(mymat)
##      [,1] [,2]   [,3]   [,4]  [,5] [,6] [,7] [,8]
## [1,]  336 53.8 1474.0 1387.2 290.3 59.3  9.7    8
## [2,]  157 22.7  570.3  570.3 300.0  0.0  0.0    0
## [3,]  193 28.5  615.5  615.5 300.0  0.0  0.0    0
## [4,]   53  5.8   73.6   73.6 300.0  0.0  0.0    0
## [5,]  185 26.8  471.8  471.8 300.0  0.0  0.0    0
## [6,]  104 13.6  172.7  172.7 300.0  0.0  0.0    0