# Creating or editing Matrix

#### hlsmith

##### Not a robit
The following code generates a correlation matrix. I would like to either edit the generated matrix (don't know how to do this but I am guessing it very easy) or create my own matrix like this one, but not using the cor function, but using a list of correlations and variables I already have. Suggestions would be appreciated? Thanks.

Code:
M<-cor(mtcars)
head(round(M,2))

#### hlsmith

##### Not a robit
My first crack at it, now I need labels...

Code:
v <- c(2,0,0,0,0,0,
0,1,0,0,0,0,
0,0,5,0,0,0,
0,0,0,6,0,0,
0,0,0,0,9,0,
0,0,0,0,0,7)
v
vv <- matrix(v,nrow=6, ncol=6)
vv

#### hlsmith

##### Not a robit
Alright the entirety:

Code:
v <- c(2,0,0,0,0,0,
0,1,0,0,0,0,
0,0,5,0,0,0,
0,0,0,6,0,0,
0,0,0,0,9,0,
0,0,0,0,0,7)

v
vv <- matrix(v,nrow=6, ncol=6)
vv
colnames(vv) <- c("EPA1", "EPA2", "EPA3", "EPA4", "EPA5", "EPA6")
rownames(vv) <- c("EPA1", "EPA2", "EPA3", "EPA4", "EPA5", "EPA6")
vv
Any suggestions on editing exiting matrices, say opening it in RStudio and manually editing?

Thanks.

#### GretaGarbo

##### Human

Code:
M<-cor(mtcars)

M[3,1]

M[3,1] <-  -1.2

#   :)

#### GretaGarbo

##### Human
But it seems we need to give an interval (like M[4:5,2:3]) to enter a matrix.

Code:
M<-cor(mtcars)

M[3,1]

M[3,1] <-  -1.2

m22 <- matrix(1:4, 2,2)
m22

M[4:5,2:3] <- m22
head(round(M,2))

#### trinker

##### ggplot2orBust
Another approach if it's a diagonal matrix (off-diagonals are all zero):

Code:
vv <- matrix(0, nrow=6, ncol=6,
dimnames = lapply(1:2, function(i) c("EPA1", "EPA2", "EPA3", "EPA4", "EPA5", "EPA6"))
)

diag(vv) <- c(2, 1, 5, 6, 9, 7)
vv

##      EPA1 EPA2 EPA3 EPA4 EPA5 EPA6
## EPA1    2    0    0    0    0    0
## EPA2    0    1    0    0    0    0
## EPA3    0    0    5    0    0    0
## EPA4    0    0    0    6    0    0
## EPA5    0    0    0    0    9    0
## EPA6    0    0    0    0    0    7