[R] Matrix problem - possibly use of 'outer'

David Orme d.orme at imperial.ac.uk
Wed Sep 3 14:00:37 CEST 2003


Hi,

I can't get my head around this - can someone give me a pointer:

I have a vector of values ('orig') representing areas and I want to 
calculate the amount contributed by each original vector values in a 
new vector ('new'). I find this hard to explain so here is a graphical 
explanation:

# CODE START
orig <- 10:1 #10 values with varying area
new <- rep(sum(orig)/6,6) #6 values with equal areas

plot(56:0, 56:0, type="n", xlim=c(56,0)) # suitable plot frame
abline(v=cumsum(c(0,new)), col="grey75") #  the original values
axis(1, at=cumsum(0:10), line=-5) # the 'breaks' for the new vector
# CODE STOP

The grey lines show how area from the vector 'orig' should fall into 
the vector 'new'. I want to represent this relationship as a 
transformation matrix (transf.mat) of dimensions length(new) x 
length(orig) such that each element in the matrix represents the shared 
areas between the vectors. colSums(transf.mat) therefore is identical 
to orig and rowSums is identical to new.

I've worked transf.mat out by hand for the example above but I need to 
find a way of computing it given the two vectors orig and new - my 
hunch is that there is some elegant way using outer but I can't work it 
out.

# CODE START
transf.mat <- matrix(c(9.167, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.833, 8.333, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0.666, 8, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
6.5, 2.667, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3.333, 5, 0.833, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 3.167, 3, 2, 1), ncol=10, byrow=T)
all.equal(colSums(transf.mat), orig)
all.equal(rowSums(transf.mat), new)
# CODE STOP

Hope this makes some sense.

David

---------------------------------------
Dr. David Orme

Department of Biological Sciences
Imperial College London
Silwood Park Campus
Ascot, Berkshire SL5 7PY UK.

Tel: +44 (0)20 759 42358
Fax: +44 (0)20 759 42339
e-mail: d.orme at imperial.ac.uk




More information about the R-help mailing list