<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content="MSHTML 5.00.2614.3500" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; David,</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; Thanks for this info. David 
James was kind enough to send me the same solution in a private 
mail.</FONT></DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; I hadn't realized that you can 
index into an array with a matrix, but I see </FONT></DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; now that this is a very useful 
tool in R and S.  </FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; Duncan Murdoch sent me a more 
concise solution:</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp;&nbsp;</FONT>&nbsp;<FONT 
face=Arial size=2>&nbsp; 
tapply(oldstock$close,list(oldstock$date,oldstock$ticker),mean)<BR><BR>&nbsp;&nbsp;&nbsp; 
which works well if the matrix "oldstock" has fewer than&nbsp;say 10,000 
rows,</FONT></DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; but which starts to bog down 
considerably for matrices with more rows.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; The solution that you and David 
James sent is still quite fast for matrices </FONT></DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; with 300,000 rows. </FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; Thanks again to everyone for 
their insights. </FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Jeff 
Miller</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<BLOCKQUOTE 
style="BORDER-LEFT: #000000 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px">
  <DIV style="FONT: 10pt arial">----- Original Message ----- </DIV>
  <DIV 
  style="BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: black"><B>From:</B> 
  <A href="mailto:David.Brahm@fmr.com" title=David.Brahm@fmr.com>Brahm, 
  David</A> </DIV>
  <DIV style="FONT: 10pt arial"><B>To:</B> <A href="mailto:jdm@xnet.com" 
  title=jdm@xnet.com>'Jeff Miller'</A> ; <A 
  href="mailto:r-help@stat.math.ethz.ch" 
  title=r-help@stat.math.ethz.ch>r-help@stat.math.ethz.ch</A> </DIV>
  <DIV style="FONT: 10pt arial"><B>Sent:</B> Monday, July 10, 2000 11:44 
AM</DIV>
  <DIV style="FONT: 10pt arial"><B>Subject:</B> RE: [R] reorganizing a data 
  frame</DIV>
  <DIV><BR></DIV>
  <DIV><FONT color=#0000ff face=Arial size=2><SPAN class=677113216-10072000>Jeff 
  Miller wants to turn a dataframe (stockdata) containing date, ticker, and 
  close into a matrix (closedata).&nbsp; Here's how I'd do it in S-Plus (sorry, 
  I haven't tried this in R):</SPAN></FONT></DIV>
  <DIV><FONT color=#0000ff face=Arial size=2><SPAN 
  class=677113216-10072000></SPAN></FONT>&nbsp;</DIV>
  <DIV><FONT color=#0000ff face=Arial size=2><SPAN 
  class=677113216-10072000>dates &lt;- 
  sort(unique(stockdata$date))</SPAN></FONT></DIV>
  <DIV><FONT color=#0000ff face=Arial size=2><SPAN 
  class=677113216-10072000>tickers &lt;- 
  sort(unique(stockdata$ticker))</SPAN></FONT></DIV>
  <DIV><FONT color=#0000ff face=Arial size=2><SPAN 
  class=677113216-10072000>closedata &lt;- matrix(NA, length(dates), 
  length(tickers), dimnames=list(as.character(dates), 
  tickers))</SPAN></FONT></DIV>
  <DIV><FONT color=#0000ff face=Arial size=2><SPAN class=677113216-10072000>idx 
  &lt;- cbind(match(stockdata$date, dates), match(stockdata$ticker, 
  tickers))</SPAN></FONT></DIV>
  <DIV><FONT color=#0000ff face=Arial size=2><SPAN 
  class=677113216-10072000>closedata[idx] &lt;- 
  stockdata$close</SPAN></FONT></DIV>
  <DIV><FONT color=#0000ff face=Arial size=2><SPAN 
  class=677113216-10072000></SPAN></FONT>&nbsp;</DIV>
  <DIV><FONT color=#0000ff face=Arial size=2><SPAN class=677113216-10072000>The 
  key here is knowing that you can index into a matrix (closedata) with an Nx2 
  matrix (idx), each row of which represents one element's coordinates.&nbsp; 
  This method is especially efficient if your matrix "closedata" is 
  sparse.</SPAN></FONT></DIV>
  <DIV><FONT color=#0000ff face=Arial size=2><SPAN 
  class=677113216-10072000></SPAN></FONT>&nbsp;</DIV>
  <DIV><FONT color=#0000ff face=Arial size=2><SPAN class=677113216-10072000>P.S. 
  The "as.character" is there because S-Plus 5.1 allows for non-character 
  dimnames, which seems foolish to me, and I use numbers for 
  dates.</SPAN></FONT></DIV>
  <P><FONT color=#000080 face=Tahoma size=2>-- David Brahm</FONT> <BR><FONT 
  color=#000080 face=Tahoma size=2>&nbsp;&nbsp;&nbsp; Fidelity 
  Investments</FONT> <BR><FONT color=#000080 face=Tahoma 
  size=2>&nbsp;&nbsp;&nbsp; (617)563-7438</FONT> </P></BLOCKQUOTE></BODY></HTML>