[R] matrix manipulations
pisicandru at hotmail.com
Mon Jan 17 20:59:07 CET 2011
I am having some difficulties with matrix operations. It is a little hard to explain it so please bear with me. I have a very large data set, large enough that it needs to be split in parts in order to deal with. I can work things on these "parts" but the problem lies in adding together these parts for the final answer.
So that been said, let's say that i split the data in 2 parts, 1 and 2. Each part has data belonging to 6 different categories, and each category has 2 different classes, these classes being the same for each category. The classes are called "land" and "water" and each category is labeled "cat1" to "cat6". I am using the command (function) table to tabulate each class for each category, but since i split the data in 2 parts, one part has only some of the 6 categories, and the other some other of the 6 categories (and not necessarily exclusive).
So let's built some results after i used the table function.
m1 <- matrix(c(32, 35, 36, 12, 15, 16), nrow = 2, ncol = 3, byrow = TRUE, dimnames = list(c("land", "water"), c("cat2", "cat5", "cat6")))
cat2 cat5 cat6
land 32 35 36
water 12 15 16
m2 <- matrix(c(45, 46, 47, 48, 21, 22, 23, 24), nrow = 2, ncol = 4, byrow = TRUE, dimnames = list(c("land", "water"), c("cat1", "cat2", "cat3", "cat4")))
cat1 cat2 cat3 cat4
land 45 46 47 48
water 21 22 23 24
So my end desired result should be a matrix (or a data frame) that has 6 columns called cat1 to cat6 and 2 rows labeled land and water, and for the category that appears in both m1 and m2 the end result will be a sum.
results will be m3:
cat1 cat2 cat3 cat4 cat5 cat6
land 45 78 47 48 35 36
water 21 34 23 24 15 16
To do this i thought in making an empty matrix for each m1 and m2 (called m01 and m02 respectively) with 6 columns and 2 rows, and do a long if else statement in which i match the name of the first column in m1 with the name of the first column in m01 and if they match get the data from m1, if not leave it 0 and so on. Same thing for m2 and m02. This is long and extremely clunky but afterwards i can add m01 with m02 and get my desired result m3. Is there any way i can do this more elegantly? My real data is split in 4 parts, but the problem is the same.
Thanks for all your inputs, and sorry for this long email, but i didn't know how else i could explain what i wanted to do.
More information about the R-help