Search code examples
rr-factor

R: Doing calculations on multiple factors/levels (Dummy variables)


I have two equally long matching vectors of time series data: Price (x) and hour (h). Hour goes from 0-23. My hour variable is my dummy variable (or factor/level variable I guess it is called in R).

Right now i've defined 24 different dummy variables, and for each hour I type my dummy variable. So for example generating 24 plots to look at or calculate 24 means etc I would type: plot.ts(hour1) # and so on for all 24.

I would like to do this for all 24 variables as easily as possible? So I can run a lot of different calculations. For example, how could I just compute the mean for all 24 dummy variables without making 24 lines of code, changing each dummy variable?

EDIT: Sorry, thought it was clear with the two vectors. Example:

 1. Price Hour
 2. 8     0
 3. 12    1
 4. 14    2
 5. 16    3
 6. 18    4
 7. 20    5
 8. 22    6
 9. 24    7
 10. 26   8
 11. 28   9
 12. 24   10
 13. 26   11
 14. 23   12
 15. 23   13
 16. 23   14
 17. 14   15
 18. 19   16
 19. 25   17
 20. 26   18
 21. 28   19
 22. 30   20
 23. 33   21 
 24. 24   22
 25. 10   23
 26. 14   0
 27. 12   1
 28. 13   2
 29. x    ect.

Solution

  • It is not clear how your data are stored since you don't give a reproducible example. I assume you have separate variables for each hour1.

    Generally, It is better to put your hourxx variable in a list to perform calculations.

    For example, this will compute mean for all hours:

        lapply(lapply(ls(pattern='hour.*'),get),mean)
    

    EDIT after OP clarification:

    You shuld create a new variable to distinguish between Hours intervals. Something like :

    dat <- data.frame(Price=rnorm(24*5),Hour=rep(0:23,5))
    dat$id <- cumsum(c(0,diff(dat$Hour)==-23))
    

    Then using ply package for example , you can compute mean by id:

    library(plyr)
    ddply(dat,.(id),summarise,mPrice=mean(Price))
    
     id     mPrice
    1  0  0.2999602
    2  1 -0.2201148
    3  2  0.2400192
    4  3 -0.2087594
    5  4  0.1666915