4

The following works fine in R

myarray <- as.array(list(c(5,5), 9, c(4,2,2,4,6)))
mydf    <- as.data.frame(myarray)

But in Splus it does not---giving the error message:

Problem in data.frameAux.list(x, na.strings = na.st..: arguments imply differing 
 number of rows: 2, 1, 5 
Use traceback() to see the call stack

Q: What's going on? How can I get this to work in Splus?

EDIT: I should make clearer why I'm going through this strange process of treating a list as a data.frame. It's because I'd eventually like to do something like the following in Splus:

mypos <- timeSeq("1/1/08", "1/3/08", by = "days")
myts <- timeSeries(data = mydf, positions = mypos)

The best feasible option right now, I guess, would be to build a list like:

mytshack <- list(mypos, as.list(myarray))

But this is clunky and I'd like to get the functionality of a timeSeries if possible

Joris Meys
  • 106,551
  • 31
  • 221
  • 263
lowndrul
  • 3,715
  • 7
  • 36
  • 54

1 Answers1

3

EDITED after comments.

SPlus does not allow vectors as values in a dataframe, contrary to R. You'll have to use a list for that, and I'd simply do :

day <- c("1/1/2000","1/2/2000","1/3/2000")
names(myarray) <- day

which allows access to the data the usual way :

> myarray[["1/1/2000"]]
[1] 5 5

Given your confirmation this is actually what you want, and the extra information about the dataset, try this :

myarray <- as.array(list(c(5,5), 9, c(4,2,2,4,6)))
mydf <- as.matrix(myarray)
colnames(mydf) <- "myarray"


mypos <- timeSeq("1/1/08", "1/3/08", by = "days")
myts <- timeSeries(data = mydf, positions = mypos)
seriesData(myts)

This works in SPlus. timeSeries needs a rectangular object, and as.rectangular can't deal with arrays. So converting to a matrix will do. Still, I'd just use the package timeSeries in R instead of hacking it together in SPlus.

Joris Meys
  • 106,551
  • 31
  • 221
  • 263
  • @Joris: Well, it was what I wanted. What I didn't mention is that the `1, 2, 3` actually correspond to calendar dates and the `5, 5` would correspond to two events, each of size 5, occurring within day 1. Eventually I'd like to turn this into a `timeSeries` object. – lowndrul Mar 17 '11 at 17:18
  • @brianjd : why use `as.data.frame` then instead of `list`? – Joris Meys Mar 17 '11 at 17:26
  • @Joris: I just edited my original question to include the background info as to what I'm treating a `list` as a `data.frame`. It's because I'd eventually like to build a `timeSeries` object from the `data.frame` and be able to do things like sub-set by date. I _could_ just put everything (including the dates) in a list like I mentioned in the edit (actually, seems like the only option right now) but accessing sub-elements of the data corresponding to dates would be clunky and require helper functions. Btw, I appreciate the discussion so far. Thank you! – lowndrul Mar 17 '11 at 17:44
  • 1
    @brianjd : then just use names, as you can use the dates in the index to select then. I've edited my answer to illustrate this. – Joris Meys Mar 17 '11 at 17:48
  • @Joris: I see. Yes, your solution of naming the elements of the array of lists with the dates seems like the better option. Very helpful. – lowndrul Mar 17 '11 at 17:58
  • 1
    @brianjd : hacked it into a timeSeries as well. see answer – Joris Meys Mar 17 '11 at 18:17
  • @Joris: Wow, that is EXACTLY what I wanted. Didn't think it was possible. Thank you so much! The reason that I'll hack this stuff together in Splus rather than using R is that the S+Finmetrics package has some very powerful functions for the handling of `timeDate` objects. R isn't quite there yet unfortunately. – lowndrul Mar 17 '11 at 19:00