I've got a character matrix with structure like the following:
dog 1 cow 9 mouse 7
bird 10 tiger 1 gnu 2
tiger 3 deer 7 deer 27
skunk 2 rat 50 NA NA
mouse 8 snake 3 NA NA
cow 7 NA NA NA NA
sheep 21 NA NA NA NA
gnu 5 NA NA NA NA
Imagine this to be a matrix of animals in locales, with data for each locale defined by successive pairs of columns. Some animals may be common between locales, but locales may also have unique animals. Ultimately I want to make a heatmap of this data, and thus need to reorder this matrix to have a structure in which there is one column for all types animals and successive columns corresponding to numbers in each locale:
dog 1 NA NA
tiger 3 1 NA
skunk 2 NA NA
mouse 8 NA NA
cow 7 9 NA
sheep 21 NA NA
gnu 5 NA 2
deer NA 7 27
rat NA 50 NA
snake NA 3 NA
mouse NA NA 7
bird 10 NA NA
In other words, I have
A1 <- c("dog", "bird", "tiger", "skunk", "mouse", "cow", "sheep", "gnu")
B1 <- as.character(c(1, 10, 3, 2, 8, 7, 21, 5))
A2 <- c("cow", "tiger", "deer", "rat", "snake", NA, NA, NA)
B2 <- as.character(c(9, 1, 7, 50, 3, NA, NA, NA))
A3 <- c("mouse", "gnu", "deer", NA, NA, NA, NA, NA)
B3 <- as.character(c(7, 2, 27, NA, NA, NA, NA, NA))
TheMatrix <- cbind(A1, B1, A2, B2, A3, B3)
and want
a1 <- c("dog", "tiger", "skunk", "mouse", "cow", "sheep", "gnu", "deer", "rat", "snake", "mouse", "bird")
b1 <- as.character(c(1, 3, 2, 8, 7, 21, 5, NA, NA, NA, NA, 10))
b2 <- as.character(c(NA, 1, NA, NA, 9, NA, NA, 7, 50, 3, NA, NA))
b3 <- as.character(c(NA, NA, NA, NA, NA, NA, 2, 27, NA, NA, 7, NA))
DesiredResult <- cbind(a1, b1, b2, b3)
Ideas on how to achieve this reorganization? It could done with loops and accounting, but surely there's a more elegant way that I'm missing.
Matrices are by default column-wise. By setting byrow as TRUE, we can arrange the data row-wise in the matrix. nrow – defines the number of rows in the R matrix. ncol – defines the number of columns in the R matrix. dimnames – takes two character arrays as input for row names and column names.
It is possible to name the rows and columns of the matrix during creation with the dimnames argument of the matrix () function. For example: We can also name the rows and columns of an R matrix after its creation by using the rownames () or colnames () functions. For example: How to access individual components of the R matrix?
Converting the Character Matrix to Numeric Matrix we will use as.numeric () & matrix () Functions. as.numeric () function: This function is used to convert a given column into a numeric value column in r language. Syntax: as.numeric (x, …) x: object to be coerced. Returns: The numeric type object in r language.
We can index the R matrix with a single vector. When using this technique, the result is a vector formed by stacking the columns of the matrix one after another. How to modify a matrix in R? We modify the R matrix by using the various indexing techniques along with the assignment operator.
library(reshape2)
ncols = ncol(TheMatrix)
nrows = nrow(TheMatrix)
dcast(as.data.frame(na.omit(cbind(c(TheMatrix[,seq(1,ncols,2)]),
c(TheMatrix[,seq(2,ncols,2)]),
rep(colnames(TheMatrix)[seq(2,ncols,2)],
each = nrows)))),
V1 ~ V3, value.var = 'V2')
# V1 B1 B2 B3
#1 bird 10 <NA> <NA>
#2 cow 7 9 <NA>
#3 deer <NA> 7 27
#4 dog 1 <NA> <NA>
#5 gnu 5 <NA> 2
#6 mouse 8 <NA> 7
#7 rat <NA> 50 <NA>
#8 sheep 21 <NA> <NA>
#9 skunk 2 <NA> <NA>
#10 snake <NA> 3 <NA>
#11 tiger 3 1 <NA>
There are a lot of things (that are each quite simple) happening here and to understand, just run each little bit on its own (starting from the inside and go out).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With