Skip to content

Instantly share code, notes, and snippets.

@geofferyzh
Created April 16, 2012 18:05
Show Gist options
  • Save geofferyzh/2400394 to your computer and use it in GitHub Desktop.
Save geofferyzh/2400394 to your computer and use it in GitHub Desktop.
RinAction - R Data Structure - Matrix
#################################
# Matrix #
#################################
# Creating Matrices
y <- matrix(1:20, nrow = 5, ncol = 4)
y
# fill by rows
cells <- c(1, 26, 24, 68)
rnames <- c("R1", "R2")
cnames <- c("C1", "C2")
mymatrix <- matrix(cells, nrow = 2, ncol = 2, byrow = TRUE, dimnames = list(rnames, cnames))
mymatrix
# fill by column
mymatrix <- matrix(cells, nrow = 2, ncol = 2, byrow = FALSE,dimnames = list(rnames, cnames))
mymatrix
# Using matrix subscripts
x <- matrix(1:10, nrow = 2)
x
x[2, ]
x[, 2]
x[1, 4]
x[1, c(4, 5)]
x[c(1,2),c(3,4)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment