So I am a self-educated coder and my convoluted scripts show it. Sorry ahead of time. Hopefully, the comments help some.

I have numerous numerical models I am running which outputs 10's of data frames each, with x, y, and z spatial coordinates. Each dataframe is created for a different point in time. I am working on a spatial analysis through time and I am trying to isolate certain features and measure their changes. I have wrangled these dataframes into a singular dataframe per numerical model. This dataframe has one column (column 1) of y-coordinates and numerous columns of z-coordinates from each different timestep (column 2:ncol()).

I have some ugly nested loops that result in a bunch of lists being generated:

# Example Data

```
transect <- structure(list(Y = c(1000, 801, 751, 701, 501, 351, 251), Z_1 = c(16.1,
31.8, 14.4, 23.7, -0.9, 21.4, 29.9), Z_2 = c(73.9, 49.9, 27.5,
21.7, -7.7, 10.4, 18.2), Z_3 = c(124.1, 73.4, 26.7, 21.7, -16.3,
12.1, 15.3), Z_4 = c(174.6, 741.7, -16.6, -24.7, -6.1, 5.5, 5.4
), Z_5 = c(220.1, 1255.2, -105.4, -77, -5.4, 7.4, 8.1), Z_6 = c(282.9,
1595.3, -164.1, -121.8, -4.6, 18, 9.1), Z_7 = c(341, 2076.9,
-262.6, -196.5, -2.6, 15.8, 8.3), Z_8 = c(403.9, 2171.8, -323.4,
-253.3, -3.2, 17.3, 11.3), Z_9 = c(462.5, 2482.4, -415.3, -329.6,
-7.9, 20, 12), Z_10 = c(523.7, 2676.1, -460.9, -387.9, -12.9,
22.6, 15.4), Z_11 = c(577, 2979.2, -551.3, -459.5, -20.5, 27,
14.3), Z_12 = c(628.7, 2960.4, -603.8, -518.8, -30.5, 26.7, 16.2
)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA,
-7L), spec = structure(list(cols = list(Y = structure(list(), class = c("collector_double",
"collector")), Z_1 = structure(list(), class = c("collector_double",
"collector")), Z_2 = structure(list(), class = c("collector_double",
"collector")), Z_3 = structure(list(), class = c("collector_double",
"collector")), Z_4 = structure(list(), class = c("collector_double",
"collector")), Z_5 = structure(list(), class = c("collector_double",
"collector")), Z_6 = structure(list(), class = c("collector_double",
"collector")), Z_7 = structure(list(), class = c("collector_double",
"collector")), Z_8 = structure(list(), class = c("collector_double",
"collector")), Z_9 = structure(list(), class = c("collector_double",
"collector")), Z_10 = structure(list(), class = c("collector_double",
"collector")), Z_11 = structure(list(), class = c("collector_double",
"collector")), Z_12 = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec")))
```

# Example Code

```
# parse through y-coordinates
for (i in transect$Y) {
# looks for elevation data within +-10 m of sea level along y-transect
if ( abs(transect$Z_3[i]) < 10) {
# checks if values in range, along transect are on one side of a mountain range
if (transect$Y[which(transect$Z_3 == transect$Z_3[i])] < transect$Y[which(transect$Z_3 == min(transect$Z_3))]) {
# returns to y-coordinate for the z-values in the range -- currently as lists that need to be merged.
# take difference between the max returned y-coordinate and the base of the mountain range
print(cbind(transect$Y[which(transect$Z_3 == transect$Z_3[i])]))
}
}
}
```

# prints

[1,] 349

[,1]

[1,] 350

[,1]

[1,] 351

[,1]

[1,] 352

[2,] 340

[,1]

[1,] 353

[,1]

[1,] 354

[2,] 56

I want to merge these lists as the loops iterate over each dataframe. I then plan to pick out the maximum returned value and do some mathing with it. **The problem is when I try to merge these I am ending up with only the last value.** I am assuming this is because I need to loop through dimensions of the object I am saving these lists too, but I can't think of a way to do this without messing up the structure of my ugly nested loops. Does anyone have any suggestions? Thank a million and let me know if this needs further clarification.

**Note:** Eventually, I need to replace the "transect$Z_3[i]" above with "transect[j][i]" so it's not just parsing through y-coordinates and analyzing elevation, but also parsing through time.

Here is what the actual data looks like when some column "Z_n" (Y-axis) is plotted against column "Y" (X-axis).

**In a nut shell, I am trying to measure the difference in "Y" values between the point at the bottom of the curve (~ -750 on the x-axis) and where the gentle curve back up first hits zero (~ -500 on the x-axis). This needs to be iterated for each "Z_n" column.**

To explain my objective graphically, I want to look at all the "Z_n" values on the basin side of the mountain -- this is the purpose of the min() in the code above. I am trying to find the first value where the basin shallows up to sea level, or in this case zero. To do this I apply the abs() to the data on the basin side to move the basin points from negative to positive. If I now look for the location of minimum"Z_n" values I should get the cell location of the surface where it crosses sea-level. I use the +-10 filter because in my actual data there is a lot of noise. From a list of "Y" values in the same row as the sea-level points found in "Z_n", the maximum of those should be the edge of this basin.

# Update

I have realized that my first code could redo some of the logic and achieve a cleaner, more accurate measurement. Below is the updated code. However, while checking the y-coordinates an error is droped and the loop is exited.

Error in if (abs(transect[i, k]) < 10) { : argument is of length zero

Any help on this would be much appreciated!

```
temp <- data.frame(matrix(NA, ncol = 1))
```

```
# loops through each column (i.e. goes through each time period)
for (k in 2:ncol(transect)) {
j = 0
print(k)
# parses through y coordinates
for (i in transect$Y) {
# checks if y-values are within +-10 from 0
if (abs(transect[i, k]) < 10) {
j = j + 1
# meant to store the y- coordinates where z-values within the 0+-10 range for a given timestep
temp[j, k] <- rbind(max(transect$Y[which(transect[,k] == transect[i, k])]))
}
}
# arranges stored y-coordiantes
# The largest of the stored values will be the point on the mountain that is near zero & the second largest
# should be the value on the other side of basin shore.
temp <- temp %>% arrange(V2)
# trying to make a dataframe that will store the basin width
basin_length[, k] <- temp[nrow(temp),] - temp[nrow(temp) - 1,]
}
```