Skip to content
Prev 342960 / 398506 Next

map column name in matrix to multiple elements

Using dput() to provide your data is enormously easier.

Assuming the original column names of your data frame correspond to
the ColMap column of the second data frame, it's very straightforward:


mat <- structure(list(A = c(0.01, -2, -4), B = c(0.2, 1.4, -3), C = c(-0.3,
2.3, -2), D = c(0.8, 3.1, 0.4), E = c(-1, -2, 2.1)), .Names = c("A",
"B", "C", "D", "E"), class = "data.frame", row.names = c("S1",
"S2", "S3"))


matnames <- structure(list(ColMap = c("A", "A", "A", "B", "B", "B", "C"),
    element = c("Apple", "Arcade", "Almira", "Boy", "Balloon",
    "Bat", "Cat")), .Names = c("ColMap", "element"), class =
"data.frame", row.names = c(NA, -7L))

mat <- mat[, matnames$ColMap]
colnames(mat) <- matnames$element



R> mat
   Apple Arcade Almira  Boy Balloon  Bat  Cat
S1  0.01   0.01   0.01  0.2     0.2  0.2 -0.3
S2 -2.00  -2.00  -2.00  1.4     1.4  1.4  2.3
S3 -4.00  -4.00  -4.00 -3.0    -3.0 -3.0 -2.0


Sarah

On Fri, Aug 8, 2014 at 9:04 AM, Adrian Johnson
<oriolebaltimore at gmail.com> wrote: