For-loop of a sequence

Hi, I need to loop this sequence so that I end up with 5 dataframes (eks_2013, eks_2014) etc. This sequence make it for 2013 alone.

(df_p_2013 and df_q_2013 are dataframes with prices and quantities for 2013)

prices <- colnames(df_p_2013)
quantities <- colnames(df_q_2013)

output_2013 <- matrix(NA, nrow=22, ncol=22)
for (tt in 1:22) {
  output_2013[[tt]] <- as.data.frame(priceIndex(prices, quantities, tt, df_tot_2013, method= "Fisher"))
}
## Extracting the columns i need from the list. 
f_2013 <- do.call(cbind.data.frame, output_2013[1:22])

## Naming rows and columns
rownames(f_2013) <- country_vector
colnames(f_2013) <- paste("base", rownames(f_2013), sep=".")

## Applying the eks-method on the fisher-matrix
eks_2013 <- eks(f_2013)
colnames(eks_2013) <- paste("base", rownames(eks_2013), sep=".")

Anyone have any ideas?

Thanks!

step 1 would be turn your code into a general function independent of 'table names with years in them'
does this work for the case of 2013?


eksfunc <- function(in1,in2,in3){
prices <- colnames(in1)
quantities <- colnames(in2)

temp_out <- matrix(NA, nrow=22, ncol=22)
for (tt in 1:22) {
  temp_out[[tt]] <- as.data.frame(priceIndex(prices, quantities, tt, in3, method= "Fisher"))
}
## Extracting the columns i need from the list. 
temp_f <- do.call(cbind.data.frame, temp_out[1:22])

## Naming rows and columns
rownames(temp_f) <- country_vector
colnames(temp_f) <- paste("base", rownames(temp_f), sep=".")

## Applying the eks-method on the fisher-matrix
eks_temp <- eks(temp_f)
colnames(eks_temp) <- paste("base", rownames(eks_temp), sep=".")
eks_temp
}

eks_2013<-eksfunc(df_p_2013,df_q_2013,df_tot_2013)
1 Like

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

Damn, that was quick! Det code worked perfectly as well! Thank you SO much :slight_smile: