I am working with rather large data sets, in which I am using a for loop to call functions where the functions have 2 nested for loops. It is very slow and I need to see if there is a way of using vectors rather than looping over observations and variables. My first data set contains 10,000+ records of the sale and transfer of goats within New Zealand. My 'main' data set contains quarterly data on every type of goat in every farm. Namely, there are 300 types of goats and ~15,000 farms. This 'main' data frame is saved as a panel data frame. Given a farm takeover - I need the goat data for the purchased farm to be added to the purchasing farms data. Here is the structure of the program I was previously using:
goat_full_takeover<-function(farm1,farm2,takeover_date){
for(loop over dates(i)){
for(loop over goat types(j)){
farm[farm(1)date(i)goat(j)] = farm[farm(1)date(i)goat(j)] +
farm[farm(2)date(i)goat(j)]
farm[farm(2)date(i)goat(j)] = NA
}
}
}
goat_partial_takeover<-function(farm1,farm2,takeover_date){
for(loop over dates(i) up to takeover_date){
for(loop over goat types(j)){
goat_scale = #goats[takeover_date + 1] / #goats[takeover_date]
farm[farm(1)date(i)goat(j)] = farm[farm(1)date(i)goat(j)] + goat_scale *
(farm[farm(2)date(i)goat(j)])
farm[farm(2)date(i)goat(j)] = (1-goat_scale)*farm[farm(2)date(i)goat(j)]
}
}
}
for(every goat farm purchased){
if(takeover == type1){
goat_full_takeover
}
if(takeover == type2){
goat_partial_takeover
}
}
So I am looping over 10,000+ goat sales, where I then loop over up to 100 quarterly dates for 300 types of goats. Is there a way I can format this differently so that I only need to loop over the purchases? Ideally, I can remove the nested for loop from the function so that the function will just stack the values for farm2 on farm1
goat1 goat2 goat3
farm1 Q1 5 5 5
farm1 Q2 6 6 6
farm1 Q3 7 7 7
farm1 Q4 8 8 8
farm2 Q1 9 9 9
farm2 Q2 10 10 10
farm2 Q3 11 11 11
farm2 Q4 12 12 12
Farm 1 buys Farm 2 >>
goat1 goat2 goat3
farm1 Q1 14 14 14
farm1 Q2 16 16 16
farm1 Q3 18 18 18
farm1 Q4 20 20 20
farm2 Q1 na na na
farm2 Q2 na na na
farm2 Q3 na na na
farm2 Q4 na na na
My issue is purely with coming up with a faster way to do this. Currently, due to the number of goats this takes almost one week to run. I need to find a streamlined way to go about this so that I can use this for other countries besides New Zealand. Thank you in advance for any help, it is greatly appreciated. If anything was not made clear, please let me know!