<!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> David,</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> 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> 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> now that this is a very useful
tool in R and S. </FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> Duncan Murdoch sent me a more
concise solution:</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> </FONT> <FONT
face=Arial size=2>
tapply(oldstock$close,list(oldstock$date,oldstock$ticker),mean)<BR><BR>
which works well if the matrix "oldstock" has fewer than say 10,000
rows,</FONT></DIV>
<DIV><FONT face=Arial size=2> but which starts to bog down
considerably for matrices with more rows.</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> The solution that you and David
James sent is still quite fast for matrices </FONT></DIV>
<DIV><FONT face=Arial size=2> with 300,000 rows. </FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> Thanks again to everyone for
their insights. </FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> Jeff
Miller</FONT></DIV>
<DIV> </DIV>
<DIV> </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). 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> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=677113216-10072000>dates <-
sort(unique(stockdata$date))</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=677113216-10072000>tickers <-
sort(unique(stockdata$ticker))</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=677113216-10072000>closedata <- 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
<- 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] <-
stockdata$close</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=677113216-10072000></SPAN></FONT> </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.
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> </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> Fidelity
Investments</FONT> <BR><FONT color=#000080 face=Tahoma
size=2> (617)563-7438</FONT> </P></BLOCKQUOTE></BODY></HTML>