I have this function:
lines$geometry = do.call(
  "c", 
  lapply(seq(nrow(b)), function(i) {
    st_sfc(
      st_linestring(
        as.matrix(
          rbind(b[i, ], e[i, ])
        )
      ),
      crs = 4326
    )
  }))
It takes point coordinates in data frame b and data frame e and adds them as line geometry to data frame lines and works just fine.
I now want to use this function on lists of several data frames. More specifically, lines_list, b_list and e_list which each contain seven data frames, called
"2005" "2006" "2007" "2008" "2009" "2010" "2012" "2011".
I tried using:
wave_years <- c("2005" "2006" "2007" "2008" "2009" "2010" "2012" "2011")
for(x in wave_years) {
lines_list[[as.character(x)]][,"geometry"] = do.call(
  "c", 
  lapply(seq(nrow(b_list[[as.character(x)]])), function(i) {
    st_sfc(
      st_linestring(
        as.matrix(
          rbind(b_list[[as.character(x)]][i, ], e_list[[as.character(x)]][i, ])
        )
      ),
      crs = 4326
    )
  }))
}
but this code gives the error
Error:
! Assigned data `do.call(...)` must be compatible with existing data.
✖ Existing data has 21797 rows.
✖ Element 1 of assigned data has 2 rows.
ℹ Only vectors of size 1 are recycled.
Backtrace:
  1. base::`[<-`(`*tmp*`, , "geometry", value = `<LINESTRING [°]>`)
 12. tibble (local) `<fn>`(`<vctrs___>`)
The first data frame ("2005") of each of the lists contains 21797 rows, but I do not know what the 2 rows in element 1 are.
I tried an approach with mapply which looks like
lines_list <- mapply(function(x, b, e){
  x %>% mutate(geometry = do.call(
  "c", 
  lapply(seq(nrow(b), function(i) {
    st_sfc(
      st_linestring(
        as.matrix(
          rbind(b[i, ], e[i, ])
        )
      ),
      crs = 4326
    )}
    ))))
}, x = lines_list, b = b_list, e = e_list, SIMPLIFY = FALSE)
which gives the error:
Error in `mutate()`:
! Problem while computing `geometry = do.call(...)`.
Caused by error in `match.fun()`:
! argument "FUN" is missing, with no default
Run `rlang::last_error()` to see where the error occurred.
I hope the provided details are sufficient to address my problem.
Example
To create my datasets lines_list, b_list and e_list, I start at my dataset lines which looks like:
structure(list(ID = 15131:15180, long.x = c(19.100531, 19.100531, 
19.100531, 19.100531, 18.706671, 18.706671, 18.706671, 18.706671, 
18.570601, 18.570601, 18.570601, 18.570601, 18.609079, 18.609079, 
18.609079, 18.609079, 20.58342, 20.58342, 20.58342, 20.58342, 
18.89217, 18.89217, 18.89217, 18.89217, 18.50174, 18.50174, 18.50174, 
18.50174, 18.497459, 18.497459, 18.497459, 18.497459, 20.05595, 
20.05595, 20.05595, 20.05595, 18.47617, 18.47617, 18.47617, 18.47617, 
18.542721, 18.542721, 18.542721, 18.542721, 18.56119, 18.56119, 
18.56119, 18.56119, 17.944201, 17.944201), lat.x = c(-33.892639, 
-33.892639, -33.892639, -33.892639, -33.463631, -33.463631, -33.463631, 
-33.463631, -33.983509, -33.983509, -33.983509, -33.983509, -33.92136, 
-33.92136, -33.92136, -33.92136, -33.231041, -33.231041, -33.231041, 
-33.231041, -32.181728, -32.181728, -32.181728, -32.181728, -33.91544, 
-33.91544, -33.91544, -33.91544, -33.98735, -33.98735, -33.98735, 
-33.98735, -33.83136, -33.83136, -33.83136, -33.83136, -34.055149, 
-34.055149, -34.055149, -34.055149, -34.008751, -34.008751, -34.008751, 
-34.008751, -34.073101, -34.073101, -34.073101, -34.073101, -33.011669, 
-33.011669), nn.idx = c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 7L, 7L, 7L, 7L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 7L, 7L, 7L, 7L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), nn.dists = c(2.87876203333951, 
2.87876203333951, 2.87876203333951, 2.87876203333951, 2.33190509250205, 
2.33190509250205, 2.33190509250205, 2.33190509250205, 2.79021190550413, 
2.79021190550413, 2.79021190550413, 2.79021190550413, 2.7395316966416, 
2.7395316966416, 2.7395316966416, 2.7395316966416, 1.82980100932888, 
1.82980100932888, 1.82980100932888, 1.82980100932888, 1.34113063086822, 
1.34113063086822, 1.34113063086822, 1.34113063086822, 2.70815622290683, 
2.70815622290683, 2.70815622290683, 2.70815622290683, 2.77742816726626, 
2.77742816726626, 2.77742816726626, 2.77742816726626, 2.11948529933567, 
2.11948529933567, 2.11948529933567, 2.11948529933567, 2.83934545175644, 
2.83934545175644, 2.83934545175644, 2.83934545175644, 2.80827041913292, 
2.80827041913292, 2.80827041913292, 2.80827041913292, 2.87513519685771, 
2.87513519685771, 2.87513519685771, 2.87513519685771, 1.73827934386191, 
1.73827934386191), long.y = c(17.905077, 17.905077, 17.905077, 
17.905077, 17.905077, 17.905077, 17.905077, 17.905077, 17.905077, 
17.905077, 17.905077, 17.905077, 17.905077, 17.905077, 17.905077, 
17.905077, 22.14605, 22.14605, 22.14605, 22.14605, 17.905077, 
17.905077, 17.905077, 17.905077, 17.905077, 17.905077, 17.905077, 
17.905077, 17.905077, 17.905077, 17.905077, 17.905077, 22.14605, 
22.14605, 22.14605, 22.14605, 17.905077, 17.905077, 17.905077, 
17.905077, 17.905077, 17.905077, 17.905077, 17.905077, 17.905077, 
17.905077, 17.905077, 17.905077, 17.905077, 17.905077), lat.y = c(-31.27383, 
-31.27383, -31.27383, -31.27383, -31.27383, -31.27383, -31.27383, 
-31.27383, -31.27383, -31.27383, -31.27383, -31.27383, -31.27383, 
-31.27383, -31.27383, -31.27383, -34.18307, -34.18307, -34.18307, 
-34.18307, -31.27383, -31.27383, -31.27383, -31.27383, -31.27383, 
-31.27383, -31.27383, -31.27383, -31.27383, -31.27383, -31.27383, 
-31.27383, -34.18307, -34.18307, -34.18307, -34.18307, -31.27383, 
-31.27383, -31.27383, -31.27383, -31.27383, -31.27383, -31.27383, 
-31.27383, -31.27383, -31.27383, -31.27383, -31.27383, -31.27383, 
-31.27383)), row.names = c(NA, -50L), class = c("tbl_df", "tbl", 
"data.frame"))
> dput(lines2[1:50,])
structure(list(ID = 15131:15180, long.x = c(19.100531, 19.100531, 
19.100531, 19.100531, 18.706671, 18.706671, 18.706671, 18.706671, 
18.570601, 18.570601, 18.570601, 18.570601, 18.609079, 18.609079, 
18.609079, 18.609079, 20.58342, 20.58342, 20.58342, 20.58342, 
18.89217, 18.89217, 18.89217, 18.89217, 18.50174, 18.50174, 18.50174, 
18.50174, 18.497459, 18.497459, 18.497459, 18.497459, 20.05595, 
20.05595, 20.05595, 20.05595, 18.47617, 18.47617, 18.47617, 18.47617, 
18.542721, 18.542721, 18.542721, 18.542721, 18.56119, 18.56119, 
18.56119, 18.56119, 17.944201, 17.944201), lat.x = c(-33.892639, 
-33.892639, -33.892639, -33.892639, -33.463631, -33.463631, -33.463631, 
-33.463631, -33.983509, -33.983509, -33.983509, -33.983509, -33.92136, 
-33.92136, -33.92136, -33.92136, -33.231041, -33.231041, -33.231041, 
-33.231041, -32.181728, -32.181728, -32.181728, -32.181728, -33.91544, 
-33.91544, -33.91544, -33.91544, -33.98735, -33.98735, -33.98735, 
-33.98735, -33.83136, -33.83136, -33.83136, -33.83136, -34.055149, 
-34.055149, -34.055149, -34.055149, -34.008751, -34.008751, -34.008751, 
-34.008751, -34.073101, -34.073101, -34.073101, -34.073101, -33.011669, 
-33.011669), nn.idx = c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 7L, 7L, 7L, 7L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 7L, 7L, 7L, 7L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), nn.dists = c(2.87876203333951, 
2.87876203333951, 2.87876203333951, 2.87876203333951, 2.33190509250205, 
2.33190509250205, 2.33190509250205, 2.33190509250205, 2.79021190550413, 
2.79021190550413, 2.79021190550413, 2.79021190550413, 2.7395316966416, 
2.7395316966416, 2.7395316966416, 2.7395316966416, 1.82980100932888, 
1.82980100932888, 1.82980100932888, 1.82980100932888, 1.34113063086822, 
1.34113063086822, 1.34113063086822, 1.34113063086822, 2.70815622290683, 
2.70815622290683, 2.70815622290683, 2.70815622290683, 2.77742816726626, 
2.77742816726626, 2.77742816726626, 2.77742816726626, 2.11948529933567, 
2.11948529933567, 2.11948529933567, 2.11948529933567, 2.83934545175644, 
2.83934545175644, 2.83934545175644, 2.83934545175644, 2.80827041913292, 
2.80827041913292, 2.80827041913292, 2.80827041913292, 2.87513519685771, 
2.87513519685771, 2.87513519685771, 2.87513519685771, 1.73827934386191, 
1.73827934386191), long.y = c(17.905077, 17.905077, 17.905077, 
17.905077, 17.905077, 17.905077, 17.905077, 17.905077, 17.905077, 
17.905077, 17.905077, 17.905077, 17.905077, 17.905077, 17.905077, 
17.905077, 22.14605, 22.14605, 22.14605, 22.14605, 17.905077, 
17.905077, 17.905077, 17.905077, 17.905077, 17.905077, 17.905077, 
17.905077, 17.905077, 17.905077, 17.905077, 17.905077, 22.14605, 
22.14605, 22.14605, 22.14605, 17.905077, 17.905077, 17.905077, 
17.905077, 17.905077, 17.905077, 17.905077, 17.905077, 17.905077, 
17.905077, 17.905077, 17.905077, 17.905077, 17.905077), lat.y = c(-31.27383, 
-31.27383, -31.27383, -31.27383, -31.27383, -31.27383, -31.27383, 
-31.27383, -31.27383, -31.27383, -31.27383, -31.27383, -31.27383, 
-31.27383, -31.27383, -31.27383, -34.18307, -34.18307, -34.18307, 
-34.18307, -31.27383, -31.27383, -31.27383, -31.27383, -31.27383, 
-31.27383, -31.27383, -31.27383, -31.27383, -31.27383, -31.27383, 
-31.27383, -34.18307, -34.18307, -34.18307, -34.18307, -31.27383, 
-31.27383, -31.27383, -31.27383, -31.27383, -31.27383, -31.27383, 
-31.27383, -31.27383, -31.27383, -31.27383, -31.27383, -31.27383, 
-31.27383), year = c(2007, 2012, 2009, 2005, 2009, 2011, 2006, 
2008, 2012, 2012, 2012, 2008, 2011, 2005, 2006, 2009, 2010, 2007, 
2008, 2012, 2011, 2010, 2011, 2007, 2005, 2007, 2011, 2005, 2008, 
2006, 2010, 2006, 2008, 2005, 2006, 2010, 2007, 2008, 2005, 2010, 
2010, 2007, 2009, 2010, 2011, 2005, 2010, 2007, 2008, 2005)), row.names = c(NA, 
-50L), class = c("tbl_df", "tbl", "data.frame"))
I create lines_list:
lines_list <- split(lines, f = lines$year)
and b_list and e_list:
b_list <- vector(mode = "list", length = length(wave_years))
names(b_list) <- wave_years
e_list <- vector(mode = "list", length = length(wave_years))
names(e_list) <- wave_years
and fill them with information
for(x in wave_years) {
b_list[[as.character(x)]] = lines_list[[as.character(x)]][, c("long.x", "lat.x")]
names(b_list[[as.character(x)]]) = c("long", "lat")
e_list[[as.character(x)]] = lines_list[[as.character(x)]][, c("long.y", "lat.y")]
names(e_list[[as.character(x)]]) = c("long", "lat")
}
This leaves me with the 3 datasets I am trying to operate on.
This is the original post I have my code from. Connecting two sets of coordinates to create lines using sf/mapview