maruschin
maruschin

Reputation: 13

R-language, convert data.frame to time series

i've a data.frame with structure:

> str(MSK)
'data.frame':   14685 obs. of  3 variables:
 $ code  : Factor w/ 423 levels "100 975","101 479",..: 281 281 281 281 281 281 281 281 281 281 ...
 $ period: Date, format: "2010-01-01" "2010-02-01" "2010-03-01" "2010-04-01" ...
 $ sales : num  515 195 414 410 551 18 373 74 62 313 ...

I need to convert this data to a matrix or another data.frame with rows: "period" and columns: "code". Fore some "code" time series are'n full. And missing value must be equal NA.

For example, i've a data like this:

code data sales
303  1day 200
303  2day 300
303  3day 400
404  2day 250
404  3day 350
606  2day 167

I need to get:

     303 404 606
1day 200  NA  NA
2day 300 250 167
3day 400 350  NA

It's not so hard to do it directly, but i want to know a right way to convert.

Sorry for my language, My native language is Russian and I'm a poor student at school.

Thank you for your attention. =)

Upvotes: 1

Views: 551

Answers (1)

SethB
SethB

Reputation: 2090

Use dcast from reshape2. http://cran.r-project.org/web/packages/reshape2/reshape2.pdf

 my_df <- data.frame(
                        period=c("1day","2day","3day","2day"),
                        code=c(303,303,304,305),
                        sale=c(100,200,300,400))
 library(reshape2)
 my_df2 <- dcast(my_df,period~code,value.var="sale")

Output:

   > my_df
      period code sale
    1   1day  303  100
    2   2day  303  200
    3   3day  304  300
    4   2day  305  400

    > my_df2
      period 303 304 305
    1   1day 100  NA  NA
    2   2day 200  NA 400
    3   3day  NA 300  NA

Upvotes: 2

Related Questions