<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META content="MSHTML 5.00.3013.2600" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<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></BODY></HTML>