Hello, this forum was very helpful yesterday with a simple question I had on
working with tables. What function will I need to use to do the following.
Move matrix a:
A B C D
x 1 2 3 4
y 5 6 7 8
to the mean of B&C and C&D:
BC CD
x 2.5 3.5
y 6.5 7.5
and then to the mean of B&CB, C&BC, C&CD, and D&CD:
AAB BAB CCD DCD
x 1.75 2.25 3.25 3.75
y 6.25 6.75 7.25 7.75
Thanks again for any help. I'm putting in some good time after work trying
to learn this language and any help would be great.
--
View this message in context:
http://r.789695.n4.nabble.com/Basic-matrix-manipulation-problem-tp4584734p4584734.html
Sent from the R help mailing list archive at Nabble.com.
On Tue, Apr 24, 2012 at 01:36:25PM -0700, Hans Thompson wrote:> Hello, this forum was very helpful yesterday with a simple question I had on > working with tables. What function will I need to use to do the following. > > Move matrix a: > > A B C D > x 1 2 3 4 > y 5 6 7 8 > > to the mean of B&C and C&D: > > BC CD > x 2.5 3.5 > y 6.5 7.5 > > and then to the mean of B&CB, C&BC, C&CD, and D&CD: > > AAB BAB CCD DCD > x 1.75 2.25 3.25 3.75 > y 6.25 6.75 7.25 7.75Hi. Try the following. # initial matrix a <- matrix(1:8, nrow=2, byrow=TRUE) dimnames(a) <- list(c("x","y"), LETTERS[1:4]) #create new matrix b <- cbind(BC=(a[,"B"]+a[,"C"])/2, CD=(a[,"C"]+a[,"D"])/2) b BC CD x 2.5 3.5 y 6.5 7.5 The next step may be done similarly using cbind(AAB=..., BAB=...). Hope this helps. Petr Savicky.
Yes. This worked very well for what I am trying to do. The only problem I
had though was figuring out why
dimnames(a) <- list(c("x","y"), LETTERS[1:4])
would not list the rows as x and y. I changed it though to
letters[24:25]
Thanks again Petr.
--
View this message in context:
http://r.789695.n4.nabble.com/Basic-matrix-manipulation-problem-tp4584734p4588042.html
Sent from the R help mailing list archive at Nabble.com.