[R] Populate matrix from data.frame

2007-06-28 Thread Andrej Kastrin
Dear all,

I have a data frame
a - data.frame(cbind(x=c('a','a','a','b','c'), 
y=c('a','b','c','d','e'),z=c(1,2,3,4,5)))
  a
  x y z
1 a a 1
2 a b 2
3 a c 3
4 b d 4
5 c e 5

and a matrix
mm - matrix(0,5,5)
colnames(mm) - c('a','b','c','d','e')
rownames(mm) - c('a','b','c','d','e')
  mm
  a b c d e
a 0 0 0 0 0
b 0 0 0 0 0
c 0 0 0 0 0
d 0 0 0 0 0
e 0 0 0 0 0

How to populate matrix in a way that first column of data frame 'a' 
correspond to rownames(mm), second column to colnames(mm) and the third 
column is the element of matrix 'mm'?

Thanks in advance,
Andrej

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Populate matrix from data.frame

2007-06-28 Thread Prof Brian Ripley
On Thu, 28 Jun 2007, Andrej Kastrin wrote:

 Dear all,

 I have a data frame
 a - data.frame(cbind(x=c('a','a','a','b','c'),
 y=c('a','b','c','d','e'),z=c(1,2,3,4,5)))
  a
  x y z
 1 a a 1
 2 a b 2
 3 a c 3
 4 b d 4
 5 c e 5

 and a matrix
 mm - matrix(0,5,5)
 colnames(mm) - c('a','b','c','d','e')
 rownames(mm) - c('a','b','c','d','e')
  mm
  a b c d e
 a 0 0 0 0 0
 b 0 0 0 0 0
 c 0 0 0 0 0
 d 0 0 0 0 0
 e 0 0 0 0 0

 How to populate matrix in a way that first column of data frame 'a'
 correspond to rownames(mm), second column to colnames(mm) and the third
 column is the element of matrix 'mm'?

mm[cbind(a$x, a$y)] - a$z

Please read about the forms of indexing matrices in 'An Introduction to 
R'.

-- 
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Populate matrix from data.frame

2007-06-28 Thread Patrick Burns
You need some caution with Brian's solution as it
depends on the matrix being in the same order as
the factors in the data frame.

a - data.frame(cbind(x=c('a','a','a','b','c'), 
y=c('a','b','c','d','e'),z=c(1,2,3,4,5)))

mm - matrix(0,5,5)
colnames(mm) - c('a','b','c','d','e')
rownames(mm) - c('a','b','c','d','e')

pp - mm[5:1, 5:1]

mm[cbind(a$x, a$y)] - a$z # desired result

pp[cbind(a$x, a$y)] - a$z # not desired result

It would be nice if the following worked:

mm[cbind(as.character(a$x), as.character(a$y))] - a$z


Patrick Burns
[EMAIL PROTECTED]
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and A Guide for the Unwilling S User)

Prof Brian Ripley wrote:

On Thu, 28 Jun 2007, Andrej Kastrin wrote:

  

Dear all,

I have a data frame
a - data.frame(cbind(x=c('a','a','a','b','c'),
y=c('a','b','c','d','e'),z=c(1,2,3,4,5)))


a
  

 x y z
1 a a 1
2 a b 2
3 a c 3
4 b d 4
5 c e 5

and a matrix
mm - matrix(0,5,5)
colnames(mm) - c('a','b','c','d','e')
rownames(mm) - c('a','b','c','d','e')


mm
  

 a b c d e
a 0 0 0 0 0
b 0 0 0 0 0
c 0 0 0 0 0
d 0 0 0 0 0
e 0 0 0 0 0

How to populate matrix in a way that first column of data frame 'a'
correspond to rownames(mm), second column to colnames(mm) and the third
column is the element of matrix 'mm'?



mm[cbind(a$x, a$y)] - a$z

Please read about the forms of indexing matrices in 'An Introduction to 
R'.

  


__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.