R Matrix to Rownames Colnames Values

R matrix to rownames colnames values

You could use the reshape2-package:

# load package
> require(reshape2)
# create an example matrix
> mdat <- matrix(c(1,2,3, 11,12,13), nrow = 2, ncol=3, byrow=TRUE,
+ dimnames = list(c("row1", "row2"),
+ c("C.1", "C.2", "C.3")))
> mdat
C.1 C.2 C.3
row1 1 2 3
row2 11 12 13
# bring matrix to long format using melt()
> melt(mdat)
Var1 Var2 value
1 row1 C.1 1
2 row2 C.1 11
3 row1 C.2 2
4 row2 C.2 12
5 row1 C.3 3
6 row2 C.3 13

How to create a matrix with specified row and col names, from a dataframe containing the data and the names

First set the row names and then convert it to matrix:

as.matrix(data.frame(dataframe[-1], row.names = dataframe$Date))

Another possibility is to use a zoo or xts object. This will create a matrix for the data and define an index attribute for the dates. The resulting object is of class zoo and can be manipulated by the functions of the zoo package.

library(zoo)
z <- read.zoo(dataframe)

Subsetting a matrix by row names and column names in R

Try to filter rows and columns in this way:

matrix[rownames(matrix)%in%list_individuals,colnames(matrix)%in%list_individuals]

Only rows and columns contained in list_individuals will be mantained in the output.

colnames and rownames with pairwise distance matrix outputs

Just stick the rownames and colnames in a dataframe alongside the data itself. "Unraveling" the matrix as a vector (and vector recycling for the names) will take care of the rest:

# example data
mat <- matrix(1:100, 10, 10)
rownames(mat) <- paste0("row",1:10)
colnames(mat) <- paste0("col",1:10)

# what you want
df <- data.frame(row = rownames(mat),
col = colnames(mat),
value = as.vector(mat) )

# take a look at the result
head(df)
# row col value
# row1 col1 1
# row2 col2 2
# row3 col3 3
# row4 col4 4
# row5 col5 5
# row6 col6 6

Renaming Rownames/Colnames of matrix `x` by matching column 1 in dataframe `y` and inserting column 3

Something like this using match ?

colnames(mat) <- metadata$Name[match(colnames(mat), metadata$Accession)]
rownames(mat) <- metadata$Name[match(rownames(mat), metadata$Accession)]

mat
# Horse Tiger Elephant Monkey
#Horse 1.00 0.50 0.25 0.1
#Tiger 0.50 1.00 0.25 0.1
#Elephant 0.25 0.50 1.00 0.1
#Monkey 0.10 0.25 0.50 1.0

Transform Names Matrix into Dataframe rows

You can use melt:

library(reshape2)
melt(mat)
head(setNames(melt(mat), c("ROWNAME", "COLNAME", "VALUE")))
# ROWNAME COLNAME VALUE
# 1 A F 1
# 2 B F 2
# 3 C F 3
# 4 D F 4
# 5 E F 5
# 6 A G 6

Extracting col/row names from a matrix based on value condition in R

If we are interested in the row/column names, then convert to table, and coerce it to a data.frame and subset

subset(as.data.frame(as.table(m)), Freq > 5, select = c(Var1, Var2))

data

m <-structure(c(0, 1.6898, 7.55815, 4.18765, 4.4806, 4.41775, 3.9795, 
4.1283, 4.255, 4.4811, 1.6898, 0, 7.67225, 4.113, 4.48225, 4.62525,
3.9288, 4.02495, 4.19675, 4.4686, 7.55815, 7.67225, 0, 7.3129,
7.23675, 7.46935, 7.29925, 7.41055, 7.4329, 7.28585, 4.18765,
4.113, 7.3129, 0, 3.8151, 3.35225, 2.886, 3.29, 3.0194, 3.949,
4.4806, 4.48225, 7.23675, 3.8151, 0, 4.2949, 3.66205, 4.0022,
3.70005, 2.34825, 4.41775, 4.62525, 7.46935, 3.35225, 4.2949,
0, 3.42355, 3.6388, 2.27245, 4.23745, 3.9795, 3.9288, 7.29925,
2.886, 3.66205, 3.42355, 0, 2.48115, 2.97045, 3.6137, 4.1283,
4.02495, 7.41055, 3.29, 4.0022, 3.6388, 2.48115, 0, 2.9763, 3.92015,
4.255, 4.19675, 7.4329, 3.0194, 3.70005, 2.27245, 2.97045, 2.9763,
0, 3.80345, 4.4811, 4.4686, 7.28585, 3.949, 2.34825, 4.23745,
3.6137, 3.92015, 3.80345, 0), .Dim = c(10L, 10L), .Dimnames = list(
c("0", "1", "2", "4", "5", "6", "7", "8", "9", "10"), c("0",
"1", "2", "4", "5", "6", "7", "8", "9", "10")))

create a matrix and row/col names in loop, in R

There are several problems with the code in the question:

  1. It is trying to insert names into the names vectors but no names have been set on the matrix.
  2. colnames(mat)[cc]<-... should be colnames(mat)[cc-3]<-
  3. Using byrow=TRUE is not an error but it is pointless since every element is 0 so it doesn't matter what order 0 is inserted.

1) Suggest doing it this way instead:

rr <- 2:8
cc <- 4:20
mat <- outer(rr, cc)
dimnames(mat) <- list(paste0("class.", rr), paste0("count.", cc))

2) Alternately, this could be done via list comprehensions using the listcompr package. For each value of r in rr the gen.named.vector call creates a named vector forming one row and then gen.named.matrix creates a matrix from the rows.

library(listcompr)
gen.named.matrix("class.{r}", gen.named.vector("count.{c}", c*r, c = cc), r = rr)

3) If you want to fix up the code in the question then do it like this:

mat <- matrix(0, nrow = 7, ncol = 17 ,
dimnames = list(character(7), character(17)))
for (rr in 2:8) {
rownames(mat)[rr-1] <- paste('class', rr, sep='.')
for(cc in 4:20) {
mat[rr-1, cc-3] <- rr * cc
colnames(mat)[cc-3] <- paste('count', cc, sep='.')
}
}

Extracting rows and columns of a matrix if row names and column names have a partial match

An easier option is to reshape to 'long' by converting to data.frame from table, and then subset the rows based on the values of 'Var1' and 'Var2'

out <- subset(as.data.frame.table(a), Var1 == sub("\\d+", "", Var2),
select =c(Var2, Freq))
with(out, setNames(Freq, Var2))
aaa1 aaa2 aaa3 bbb1 bbb2 bbb3 ccc1 ccc2 ccc3
0.01495641 1.57504185 2.32762287 0.42652979 0.41329383 0.07119408 0.64530516 1.39629918 0.17042160

Or with row/column indexing

i1 <- match( sub("\\d+", "", colnames(a)), rownames(a))
a[cbind(i1, seq_along(i1))]
[1] 0.01495641 1.57504185 2.32762287 0.42652979 0.41329383 0.07119408 0.64530516 1.39629918 0.17042160


Related Topics



Leave a reply



Submit