I am struggling with efficient data handling in the following code. The code produces my desired outcome, a data frame with two variables, but it is incredibly slow because