[R] casting help please

davidr at rhotrading.com davidr at rhotrading.com
Fri Sep 5 20:20:44 CEST 2008


I think I just remembered that in a melted data.frame the values have to
be called 'value' and tried renaming my third column so. Now it works
(without add.missing).

Thanks for this powerful piece of software!
-- David
(I was trying to convert a set of data in 'long' format to 'wide'
format. Basically I have a pretty long price history for many tickers
basically stacked up as indicated in the toy example and want them
reshaped with each ticker in a column with dates in the first column.
Maybe cast was not the best tool? But it works now.)

-----Original Message-----
From: hadley wickham [mailto:h.wickham at gmail.com] 
Sent: Friday, September 05, 2008 1:10 PM
To: David Reiner <davidr at rhotrading.com>
Cc: r-help at r-project.org
Subject: [SPAM] - Re: [R] casting help please - Found word(s) list error
in the Text body

Hi David,

Does getting rid of the missings help?

prices <- prices[complete.cases(prices), ]

Also, you shouldn't need  add.missing=TRUE - what are you trying to do?

Hadley

On Fri, Sep 5, 2008 at 12:12 PM,  <davidr at rhotrading.com> wrote:
> I have a data.frame which I believe is melted already and am having
> trouble casting it to 'wide' format.
>
> It looks something like
>
>
>
>> (x <- data.frame(ticker=c(rep("A",5),rep("B",6)), date=c(1:5, 1:6),
> value=c(NA,100*exp(rnorm(10,0,.1)))))
>
>> cast(x, date ~ ticker) # this does what I want with toy data
>
>
>
> But when I use my real data frame
>
>
>
>> str(prices)
>
> 'data.frame':   308188 obs. of  3 variables:
>
>  $ Ticker: chr  "ticker1" " ticker1" " ticker1" " ticker1" ...
>
>  $ Date    :Class 'Date'  num [1:308188] 12296 12297 12298 12299 12300
> ...
>
>  $ Price   : num  NA NA NA NA NA NA NA NA NA NA ...
>
>
>
> I get
>
>
>
>> prices.wide <- cast(prices, Date ~ Ticker, add.missing=TRUE)
>
> Error in data.frame(data[, c(variables), drop = FALSE], result =
> data$value) :
>
>  arguments imply differing number of rows: 308188, 0
>
>
>
> (I tried various other arguments to cast - all gave the same error
> message.)
>
> It is a fact that the various tickers have data for different date
> ranges in the data frame and there are lots of NA's,
>
> but the toy example above has different date ranges for the two
tickers
> and an NA, so I don't know what else to look for in my data,
>
> or what args to cast might make it work.
>
>
>
> Any insights or direction would be much appreciated.
>
>
>
> David L. Reiner, PhD
>
> Head Quant
>
> Rho Trading Securities, LLC
>
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.
>



-- 
http://had.co.nz/



More information about the R-help mailing list