Bhaskar Mitra
2021-Jun-08 18:10 UTC
[R] Converting data from weekly interval to daily interval
Hello Everyone, I have data at weekly intervals. The data structure is something like this. df1 <- date a 1/7/2020 3 1/14/2020 6 1/21/2021 7 I would like to convert the data from the weekly interval to the daily interval. Something like this. date a 1/7/2020 0.5 1/8/2020 0.3 1/9/2020 0.7 I am using the following codes d1 <- nrow(df1) df2 <- data.frame(matrix(NA, nrow = (d1)*7, ncol = 1) inc <- 0 for (i in 1:nrow(df1)) { for (j in 1:7) { inc <- inc + 1 df2[inc,1] <- df1[i,1]/7 } } rm(df1) However, the sum of the daily values generated in df2 differ from the original value in the df1 data frame. There is a 1-2% discrepancy. I would appreciate it if someone could suggest how to improve the codes. Thanks, Bhaskar [[alternative HTML version deleted]]
Jim Lemon
2021-Jun-08 22:39 UTC
[R] Converting data from weekly interval to daily interval
Hi Bhaskar, Perhaps you are looking for this: approx(c(3,6,7),n=21) $x [1] 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 [20] 2.9 3.0 $y [1] 3.0 3.3 3.6 3.9 4.2 4.5 4.8 5.1 5.4 5.7 6.0 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 [20] 6.9 7.0 This assumes that you are taking each value as a snapshot reading rather than a cumulation of the values. Jim On Wed, Jun 9, 2021 at 4:33 AM Bhaskar Mitra <bhaskar.kolkata at gmail.com> wrote:> > Hello Everyone, > > I have data at weekly intervals. > The data structure is something like this. > > df1 <- > date a > 1/7/2020 3 > 1/14/2020 6 > 1/21/2021 7 > > I would like to convert the data from the weekly > interval to the daily interval. Something like this. > > date a > 1/7/2020 0.5 > 1/8/2020 0.3 > 1/9/2020 0.7 > > > I am using the following codes > > d1 <- nrow(df1) > > df2 <- data.frame(matrix(NA, nrow = (d1)*7, ncol = 1) > > inc <- 0 > for (i in 1:nrow(df1)) { > for (j in 1:7) { > inc <- inc + 1 > df2[inc,1] <- df1[i,1]/7 > } > } > rm(df1) > > > > However, the sum of the daily values generated in df2 differ > from the original value in the df1 data frame. There is a 1-2% discrepancy. > I would appreciate it if someone could suggest how to improve the codes. > > Thanks, > Bhaskar > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Greg Minshall
2021-Jun-09 02:30 UTC
[R] Converting data from weekly interval to daily interval
Bhaskar, not your main question, but ... given that you have column names, you might think of : df2[inc,"date"] <- df1[i,"date"]/7 or : df2[inc,]$date <- df1[i,]$date/7 i.e., using your column names instead of numeric indices. then, also, instead of a for loop, possibly you can use R's nicely vectorized operations. i.e., something like : df2$date <- df2$date/7 nb: i haven't tried to run the above code, so no guarantees. (again, if i've typed that correctly -- i haven't tested -- you will end up with the same answers you *don't* want, but maybe more idiomatically.) cheers, Greg