[R] map column name in matrix to multiple elements
Sarah Goslee
sarah.goslee at gmail.com
Fri Aug 8 15:14:02 CEST 2014
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:
> Hi:
> I am requesting help on matrix mapping.
>
> I have a matrix that is 5000 rows x 3000 columns in R env.
>
> Matrix:
>
> A B C D E
>
> S1 0.01 0.2 -0.3 0.8 -1
>
> S2 -2 1.4 2.3 3.1 -2
>
> S3 -4 -3 -2 0.4 2.1
>
>
>
> I have another excel sheet, which when I import will have two columns..
>
>
> ColMap element
>
> A Apple
> A Arcade
> A Almira
> B Boy
> B Balloon
> B Bat
> C Cat
> .....
>
>
> I want to create another matrix with elements as column names and map
> ColMap values onto elements.
>
>
>
> new matrix:
>
> Apple Arcade Almira Boy Ballon ....
> S1 0.01 0.01 0.01 0.2 0.2
> S2 -2 -2 -2 1.4 1.4....
> S3 -4 -4 -4 -3 -3 ..
>
>
> I have no idea how I could do this in R. would any one help please.
>
> Thanks
>
--
Sarah Goslee
http://www.functionaldiversity.org
More information about the R-help
mailing list