简体   繁体   中英

R: parallel calculation of xts. file and the vectors

I want to create a loop of this parallel calculation between a.xts (ETRp with one column) and the vectors. expecting the result with additional column.

xts file is ETRp

head(ETRp)
                     ETR[mm/day]
2020-11-01 00:00:00 0.007515922
2020-11-01 00:01:00 0.002171158
2020-11-01 00:02:00 0.005164759
2020-11-01 00:03:00 0.00495627

For example the vectors are:

SCF= c(1,2,3)
Ab= c(1,2,3)

The formula that I want to apply is this.

EIp<- SCF*Ab*ETRp

So tried to do the parallel calculation manually:

EIp<-SCF[1]*Ab[1]*ETRp
names(EIp)[1] <-"F1_1"

EIp$"F1_2"<-SCF[2]*Ab[2]*ETRp
EIp[,2]<-SCF[2]*Ab[2]*ETRp
EIp$"F1_3"<-SCF[3]*Ab[3]*ETRp
head(EIp)
                        F1_1        F1_2        F1_3
2020-11-01 00:00:00 0.007515922 0.007730099 0.010083984
2020-11-01 00:01:00 0.002111002 0.002171158 0.002832295
2020-11-01 00:02:00 0.003849464 0.003959160 0.005164759
2020-11-01 00:03:00 0.004956272 0.005097508 0.006649745
2020-11-01 00:04:00 0.006465297 0.006649535 0.008674379
2020-11-01 00:05:00 0.006972469 0.007171160 0.009354842

but this is very much time consuming so,

Expecting the result with additional column, I tried using FOR loop, (below)

for(i in 1:3){
EIp[,i]<-SCF[i]*Ab[i]*ETRp
head(EIp)
                    ETR[mm/day]
2020-11-01 00:00:00 0.007515922
2020-11-01 00:01:00 0.002171158
2020-11-01 00:02:00 0.005164759
2020-11-01 00:03:00 0.004956272
2020-11-01 00:04:00 0.006649535
2020-11-01 00:05:00 0.009354842

But the result only shows one column.

IN SHORT,

THIS IS WHAT I WANT using Loop

                        F1_1        F1_2        F1_3
2020-11-01 00:00:00 0.007515922 0.007730099 0.010083984
2020-11-01 00:01:00 0.002111002 0.002171158 0.002832295
2020-11-01 00:02:00 0.003849464 0.003959160 0.005164759
2020-11-01 00:03:00 0.004956272 0.005097508 0.006649745
2020-11-01 00:04:00 0.006465297 0.006649535 0.008674379
2020-11-01 00:05:00 0.006972469 0.007171160 0.009354842

BUT, THIS IS WHAT I GET

head(EIp)
                    ETR[mm/day]
2020-11-01 00:00:00 0.007515922
2020-11-01 00:01:00 0.002171158
2020-11-01 00:02:00 0.005164759
2020-11-01 00:03:00 0.004956272
2020-11-01 00:04:00 0.006649535
2020-11-01 00:05:00 0.009354842

Why do I have only one column in the result?

How can I get expected result (1 column XTS and vectors with 3 elements)? I expect it to be 3 columns ( with additional columns) Can you please help me with this XTS and vector calculation?

It's best practice to pre-allocate (create) the result object when you use for loops in R. Then you fill in the elements with the correct values inside the loop.

# setup example
library(xts)
data(sample_matrix)                                                                        
ETRp <- as.xts(sample_matrix, dateFormat = "Date")[, "Close"]
SCF <- c(1, 2, 3) 
Ab <- c(1, 2, 3) 
SCF_Ab <- SCF * Ab

# create the result object
EIp <- matrix(NA, nrow = nrow(ETRp), ncol = length(SCF_Ab))
EIp <- xts(EIp, index(ETRp))
colnames(EIp) <- paste0("F1_", seq_along(SCF_Ab))

# fill in the results
for (i in seq_along(SCF_Ab)) {
    EIp[,i] <- ETRp * SCF_Ab[i]
}
 
head(EIp)
##                F1_1     F1_2     F1_3
## 2007-01-02 50.11778 200.4711 451.0600
## 2007-01-03 50.39767 201.5907 453.5790
## 2007-01-04 50.33236 201.3294 452.9912
## 2007-01-05 50.33459 201.3384 453.0114
## 2007-01-06 50.18112 200.7245 451.6301
## 2007-01-07 49.99185 199.9674 449.9267

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM