Making a FOR-loop of this process

Hi, everyone!

I want to make the following operation for the years 2013-2017. The code I have written here works very well for 2017, but I don't to write almost exactly the same thing for all years. Therefore I was thinking of making a FOR-loop. The only thing I would have to do to end up with a data.frame for all years would be to change the 2017 to any other year.

Does anyone have an idea of how to do this?

Merging datasets:

T <- data.frame(equipment_pct$2017, infrastructure_pct$2017, other_pct$2017, personnel_pct$2017, pwt91_xr$2017,
poecd_ppp_series$2017, oecd_ppp_series$2017, w$2017)

Changing the variable names

names(T) <- c("qequip_2017", "qinfra_2017", "qother_2017", "qpers_2017", "pequip_2017", "pinfra_2017", "pother_2017", "ppers_2017")

Data frame with quantities

Tq <- data.frame(equipment_pct$2017, infrastructure_pct$2017, other_pct$2017, personnel_pct$2017)

Data frame with prices

Tp <- data.frame(pwt91_xr$2017, poecd_ppp_series$2017, oecd_ppp_series$2017, w$2017)

Changing variable names

names(Tp) <- c("pequip_2017", "pinfra_2017", "pother_2017", "ppers_2017")

Vector with quantity names

names(Tq) <- c("qequip_2017", "qinfra_2017", "qother_2017", "qpers_2017")

Vectors with names of prices and quantities

prices <- colnames(Tp)
quantities <- colnames(Tq)

Making a FOR-loop to compute the fisher index for all countries as a base. This will be stored in a list in the global environment.

output <- matrix(NA, nrow=22, ncol=22)
for (tt in 1:22) {
output[[tt]] <- as.data.frame(priceIndex(prices, quantities, tt, T, method= "Fisher"))
}

Extracting the columns i need from the list.

f_2017 <- do.call(cbind.data.frame, output[1:22])

Naming rows and columns

rownames(f_2017) <- country_vector
colnames(f_2017) <- paste("base", rownames(f_2017), sep=".")

Applying the eks-method on the fisher-matrix

eks_2017 <- eks(f_2017)
colnames(eks_2017) <- paste("base", rownames(eks_2017), sep=".")

So, I would like to end up with 5 data frames, called eks_2013, eks_2014 etc...

I was thinking of something like this to start with:

Merging datasets:

year <- c(2013:2017)
for (tt in year) {
T[tt] <- data.frame(equipment_pct$tt, infrastructure_pct$tt, other_pct$tt, personnel_pct$tt, pwt91_xr$tt,
oecd_ppp_series$tt, oecd_ppp_series$tt, w$tt)
}

But I can't make it work.

Thanks! :slight_smile:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.