[R] reorganizing a data frame

Duncan Murdoch dmurdoch at pair.com
Sat Jul 8 16:35:03 CEST 2000


On Sat, 8 Jul 2000 06:58:29 -0500, you wrote:

>    Thanks for the response! I'm not sure that
>    I completely understand your suggestion.
>
>    I start with  a dataframe called "oldstock"
>    (three columns: date, ticker, close) and I want to
>    turn this into a matrix called "newstock" with
>length(unique(stockdata[,"date"]))
>    rows and length(unique(stockdata[,"ticker"])) columns.
>    The names of the rows will be the dates, and the names of
>    the columns will be the tickers, and the value of
>    will either be the close price for the ticker on the date
>    in question, or NA if we don't have a close price for the
>    ticker on that date.
>
>    I'm not that familiar with how tapply works so
>    I don't see exactly how to apply your idea to "oldstock"
>    in order to get out "newstock". What should
>    the arguments of tapply be?

>> The tapply() function is what you want.  I think the code would be
>>
>> tapply(INTC,list(date,ticker),INTC,mean)

Change this to 

  tapply(oldstock$close,list(oldstock$date,oldstock$ticker),mean)

I had a typo in what I wrote before:  INTC should only have appeared
once.  (It happened because I couldn't remember the order of the args,
and when it didn't work the first way, I moved INTC to the start, but
forgot to delete the second entry.)

The basic use of tapply for a 2 way table takes these arguments:

  tapply( x,    #  The variable to do the calculations on.  
            list(rowvar, colvar), # The variables to determine the row
and column
            func)  # The function that gives the table entry

This produces a table with row labels from rowvar, column labels from
colvar.  The table entry at row r column c is func(x[rowvar=r &
colvar=c]).

I hope this helps.

Duncan Murdoch
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list