I have a big chunk of Excel Data with 4260 lines which I want to process using R (and the XLConnect package). There are 43 different districts in these 4260 lines, each district containing 98 lines. Instead of repeating a chunk of similar code for every district 43 times, I want to use a loop going through all lines and storing specific data (columns) into vectors.
Here is an abstract of my initial primitive code:
wb = loadWorkbook("Bel_housing_prices_disctrict.xlsx")
#--------------Malines (district 1)
df_Malines = readWorksheet(wb, sheet="Par arrondissement", startRow = 103, endRow = 201, startCol = 0, endCol = 0)
maisons_total_price_Malines <- df_Malines$Col8
maisons_price_mean_Malines <- df_Malines$Col10
villas_total_price_Malines <- df_Malines$Col18
villas_price_mean_Malines <- df_Malines$Col20
#--------------Turnhout (district 2)
df_Turnhout = readWorksheet(wb, sheet="Par arrondissement", startRow = 202, endRow = 300, startCol = 0, endCol = 0)
maisons_total_price_Turnhout <- df_Turnhout$Col8
maisons_price_mean_Turnhout <- df_Turnhout$Col10
villas_total_price_Turnhout <- df_Turnhout$Col18
villas_price_mean_Turnhout <- df_Turnhout$Col20
#-------------- (district 3)
What changes at every district (Malines, Turnhout,... etc.) is the "startRow"- and the "endRow"- value, which increase by the number 99 until the last line 4260 is reached. I thought of writing a loop that looks somehow like this:
i=103
for (n in c("Malines","Turnhout","district3",...))
{
df_"n" = readWorksheet(wb, sheet="Par arrondissement", startRow = i, endRow = i+98, startCol = 0, endCol = 0)
maisons_total_price_"n" <- df_"n"$Col8
...
i=i+99
}
But of course, this loop/function does not work because I did it wrong and I could not find a solution so far... It is merely an "idea" for a function. The function (loop) would create new variables with my data "on the go", by runnning through all 4260 lines. If my idea of saving this data do different variables is wrong, I am glad for every alternative solution (list?)?
I hope I made my problem clear and I am glad for any tips on a neat solution!
Best regards