Reputation: 397
I´m working with the following data:
year month day fivemin rrp_nsw rrp_qld rrp_sa rrp_tas rrp_vic
2009 7 1 1 16.9 17.6 16.7 15.7 15.5
2009 7 1 2 17.7 18.8 17.8 16.1 15.5
2009 7 1 3 17.7 18.6 18.1 15.9 15.4
2009 7 1 4 16.7 18.6 17.6 14.3 12.8
2009 7 1 5 15.6 17.6 16.3 13.2 11.8
2009 7 2 1 13.7 15.7 12.0 11.1 12.9
2009 7 2 2 13.7 15.8 11.9 11.1 12.9
2009 7 2 3 13.9 16.1 12.1 11.2 12.9
2009 7 2 4 13.8 16.0 12.2 11.2 12.8
2009 7 2 5 13.7 16.3 11.6 10.6 12.6
2009 7 3 1 13.7 15.8 11.9 11.0 12.7
2009 7 3 2 13.8 16.0 12.1 11.2 12.9
2009 7 3 3 17.6 17.6 17.3 16.5 17.1
2009 7 3 4 17.7 17.6 17.3 16.8 17.4
2009 7 3 5 15.8 16.0 15.1 15.0 15.5
The idea is calculate the variation for rrp_nsw, rrp_qld, rrp_sa, rrp_tas y rrp_vic and square them. I was using the following line
(Base[-1,5:9] - Base[-nrow(Base),5:9])^2
But, I need to apply this line each time that the variable day changes. I want to construct the following matrix:
year month day fivemin rrp_nsw rrp_qld rrp_sa rrp_tas rrp_vic V1 V2 V3 V4 V5
2009 7 1 1 16.9 17.6 16.7 15.7 15.5 NA NA NA NA NA
2009 7 1 2 17.7 18.8 17.8 16.1 15.5 0.59 1.35 1.19 0.15 0.00
2009 7 1 3 17.7 18.6 18.1 15.9 15.4 0.00 0.04 0.09 0.02 0.01
2009 7 1 4 16.7 18.6 17.6 14.3 12.8 0.89 0.00 0.25 2.65 6.64
2009 7 1 5 15.6 17.6 16.3 13.2 11.8 1.20 0.92 1.66 1.06 1.03
2009 7 2 1 13.7 15.7 12.0 11.1 12.9 NA NA NA NA NA
2009 7 2 2 13.7 15.8 11.9 11.1 12.9 0.00 0.01 0.00 0.00 0.00
2009 7 2 3 13.9 16.1 12.1 11.2 12.9 0.02 0.08 0.03 0.00 0.00
2009 7 2 4 13.8 16.0 12.2 11.2 12.8 0.00 0.01 0.02 0.00 0.01
2009 7 2 5 13.7 16.3 11.6 10.6 12.6 0.03 0.11 0.39 0.35 0.07
2009 7 3 1 13.7 15.8 11.9 11.0 12.7 NA NA NA NA NA
2009 7 3 2 13.8 16.0 12.1 11.2 12.9 0.02 0.02 0.04 0.03 0.03
2009 7 3 3 17.6 17.6 17.3 16.5 17.1 14.06 2.86 27.70 29.04 17.53
2009 7 3 4 17.7 17.6 17.3 16.8 17.4 0.02 0.00 0.00 0.08 0.09
2009 7 3 5 15.8 16.0 15.1 15.0 15.5 3.77 2.88 4.82 3.31 3.52
I would be grateful if someone can explain how apply a for or if.
Upvotes: 1
Views: 40
Reputation: 160447
Here's a dplyr
method:
library(dplyr)
Base %>%
arrange(year, month, day, fivemin) %>%
group_by(year, month, day) %>%
mutate_at(vars(rrp_nsw, rrp_qld, rrp_sa, rrp_tas, rrp_vic), list(d = ~ c(NA, diff(.)^2))) %>%
ungroup()
# # A tibble: 15 x 14
# year month day fivemin rrp_nsw rrp_qld rrp_sa rrp_tas rrp_vic rrp_nsw_d rrp_qld_d rrp_sa_d rrp_tas_d rrp_vic_d
# <int> <int> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 2009 7 1 1 16.9 17.6 16.7 15.7 15.5 NA NA NA NA NA
# 2 2009 7 1 2 17.7 18.8 17.8 16.1 15.5 0.64 1.44 1.21 0.16 0
# 3 2009 7 1 3 17.7 18.6 18.1 15.9 15.4 0 0.0400 0.09 0.04 0.01000
# 4 2009 7 1 4 16.7 18.6 17.6 14.3 12.8 1 0 0.25 2.56 6.76
# 5 2009 7 1 5 15.6 17.6 16.3 13.2 11.8 1.21 1 1.69 1.21 1
# 6 2009 7 2 1 13.7 15.7 12 11.1 12.9 NA NA NA NA NA
# 7 2009 7 2 2 13.7 15.8 11.9 11.1 12.9 0 0.01 0.01000 0 0
# 8 2009 7 2 3 13.9 16.1 12.1 11.2 12.9 0.04 0.09 0.0400 0.01000 0
# 9 2009 7 2 4 13.8 16 12.2 11.2 12.8 0.01000 0.01 0.01000 0 0.01000
# 10 2009 7 2 5 13.7 16.3 11.6 10.6 12.6 0.01 0.09 0.360 0.360 0.04
# 11 2009 7 3 1 13.7 15.8 11.9 11 12.7 NA NA NA NA NA
# 12 2009 7 3 2 13.8 16 12.1 11.2 12.9 0.01 0.0400 0.0400 0.0400 0.04
# 13 2009 7 3 3 17.6 17.6 17.3 16.5 17.1 14.4 2.56 27.0 28.1 17.6
# 14 2009 7 3 4 17.7 17.6 17.3 16.8 17.4 0.01000 0 0 0.09 0.0900
# 15 2009 7 3 5 15.8 16 15.1 15 15.5 3.61 2.56 4.84 3.24 3.61
(The numbers are slightly different than what you have, perhaps I've misunderstood or your question was just a demo with placeholders.)
Upvotes: 1