Try this:

xtabs(x ~ t + id, data = X)


On Wed, Jul 1, 2009 at 2:35 PM, Young Cho <young.s...@gmail.com> wrote:

> Hi, thanks everyone for any help in advance.
>
> I found myself dealing with a tabular time-series data formatted  each row
> like [ time stamp,   ID, values]. I made a small examples:
>
> X = data.frame(t=c(1,1,1,2,2,2,2,3,3,3,4,4,4,5,5),id =
> c('a','b','c','c','b','d','e','b','a','e','a','b','d','b','c'))
> X$x = rnorm(15)
>
> 't' is time stamp, 'id' is identifier, 'x' is time series values. They are
> not necessarily ordered and have sometimes missing values. In order to do
> any analysis, I used to convert this type of data into a matrix form :
>
> Y = matrix(NA,length(unique(X$id)),length(unique(X$t)))
> rownames(Y) = sort(unique(X$id))
> colnames(Y) = sort(unique(X$t))
> for(i in 1:nrow(Y)){
>   xi = X[ X$id == rownames(Y)[i], ]
>   Y[i, match(xi$t, colnames(Y)) ] = xi$x
> }
>
> Then, run any R operations on Y. Now, this conversion gets very painfully
> slow as my data gets substantially larger. I was wondering if there is some
> better ways to convert a table like 'X' into a matrix like 'Y', or even
> better ways to re-format data, not necessarily matrix form.
>
> Young
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to