title: How to set the crs on a list of shapefiles (sf)?
body: <p>I am reading in a list of shapefiles (as <code>sf</code>) and I want to set the <code>crs</code>. I know this can easily be done for one shapefile (e.g., <a href="https://stackoverflow.com/questions/61590209/how-to-set-crs-for-a-shp-polygon-file-in-r">here</a>), but I have not found a solution for multiple or a list of shapefiles.</p>
  <p>I am reading in the shapefiles with <code>list.files</code> and <code>lapply</code>.</p>
  <pre><code>library(sf)
  
  # Example (Not Run)
  temp = list.files(path = &quot;/data&quot;,
                    pattern = &quot;*WEST.shp$&quot;,
                    full.names = TRUE)
  
  # Then, apply the st_read function to the list of shapefiles (Not Run).
  myfiles = lapply(temp, st_read)
  </code></pre>
  <p>If reading in 1 shapefile, then you can simply set the crs by:</p>
  <pre><code>boundary &lt;- st_read(
           &quot;/data/tsugWEST.shp&quot;, crs = 4326)
  </code></pre>
  <p>However, if I do it with <code>lapply</code>, then it returns the following error:</p>
  <blockquote>
  <p>Error in st_read.default(crs = 4326): dsn should specify a data
  source or filename</p>
  </blockquote>
  <p><strong>Sample Data</strong> (i.e., list of shapefiles after being read into R)</p>
  <pre><code>shapefiles &lt;- list(abieWEST.shp = structure(list(AREA = 1, PERIMETER = 0, ABIELASI_ = 0, 
      ABIELASI_I = 0, CODE = 0L, geometry = structure(list(structure(list(
          structure(c(-126.623274277836, -122.269490649612, -117.311014850802, 
          -114.529430866103, -113.803800261399, -112.836292788461, 
          -108.603447594355, -103.765910229662, -102.193710586137, 
          -101.105264679081, -101.58901841555, -107.515001687299, 
          -115.255061470807, -117.552891719036, -125.897643673132, 
          -130.49330416959, -141.740578542501, -146.940931209546, 
          -144.643100961316, -136.056472138987, -126.623274277836, 
          63.8736898397207, 61.9386748938436, 55.5289378856255, 
          55.4079994515082, 54.0776766762177, 52.9892307691618, 
          48.9982624432902, 45.3701094197706, 42.8304023033068, 
          36.7834805974407, 28.3177902092282, 23.8430681468874, 
          24.3268218833566, 27.834036472759, 35.5740962562675, 
          49.2401393115248, 55.6498763197429, 63.1480592350168, 
          65.4458894832459, 65.4458894832459, 63.8736898397207), .Dim = c(21L, 
          2L))), class = c(&quot;XY&quot;, &quot;POLYGON&quot;, &quot;sfg&quot;))), n_empty = 0L, crs = structure(list(
          input = NA_character_, wkt = NA_character_), class = &quot;crs&quot;), class = c(&quot;sfc_POLYGON&quot;, 
      &quot;sfc&quot;), precision = 0, bbox = structure(c(xmin = -146.940931209546, 
      ymin = 23.8430681468874, xmax = -101.105264679081, ymax = 65.4458894832459
      ), class = &quot;bbox&quot;))), row.names = c(NA, -1L), class = c(&quot;sf&quot;, 
  &quot;data.frame&quot;), sf_column = &quot;geometry&quot;, agr = structure(c(AREA = NA_integer_, 
  PERIMETER = NA_integer_, ABIELASI_ = NA_integer_, ABIELASI_I = NA_integer_, 
  CODE = NA_integer_), class = &quot;factor&quot;, .Label = c(&quot;constant&quot;, 
  &quot;aggregate&quot;, &quot;identity&quot;))), acerWEST.shp = structure(list(AREA = 1, 
      PERIMETER = 0, ABIELASI_ = 0, ABIELASI_I = 0, CODE = 0L, 
      geometry = structure(list(structure(list(structure(c(-108.146180374448, 
      -125.155521463775, -140.514702596675, -134.929545821075, 
      -128.963582901684, -126.805681420203, -124.647779938721, 
      -123.886167651139, -120.458912357021, -120.458912357021, 
      -114.746820200157, -111.573435668566, -108.526986518239, 
      -107.003761943076, -107.003761943076, -104.591989699066, 
      -104.084248174012, -102.561023598848, -104.845860461594, 
      -102.053282073794, -98.1182852546211, -94.310223816712, -108.146180374448, 
      15.5862714176048, 37.6730277574772, 57.9826887596588, 60.013654859877, 
      55.9517226594406, 56.9672057095497, 55.5709165156497, 55.6978518969134, 
      56.0786580407043, 54.3015627030134, 51.2551135526862, 47.827858258568, 
      47.9547936398317, 46.5585044459317, 45.416086014559, 42.2427014829681, 
      37.6730277574772, 37.1652862324227, 34.1188370820955, 30.3107756441864, 
      30.5646464067137, 26.2488434437501, 15.5862714176048), .Dim = c(23L, 
      2L))), class = c(&quot;XY&quot;, &quot;POLYGON&quot;, &quot;sfg&quot;))), n_empty = 0L, crs = structure(list(
          input = NA_character_, wkt = NA_character_), class = &quot;crs&quot;), class = c(&quot;sfc_POLYGON&quot;, 
      &quot;sfc&quot;), precision = 0, bbox = structure(c(xmin = -140.514702596675, 
      ymin = 15.5862714176048, xmax = -94.310223816712, ymax = 60.013654859877
      ), class = &quot;bbox&quot;))), row.names = c(NA, -1L), class = c(&quot;sf&quot;, 
  &quot;data.frame&quot;), sf_column = &quot;geometry&quot;, agr = structure(c(AREA = NA_integer_, 
  PERIMETER = NA_integer_, ABIELASI_ = NA_integer_, ABIELASI_I = NA_integer_, 
  CODE = NA_integer_), class = &quot;factor&quot;, .Label = c(&quot;constant&quot;, 
  &quot;aggregate&quot;, &quot;identity&quot;))))
  </code></pre>
  <p>For 1 shapefile in the list, I can easily set the <code>crs</code> by doing:</p>
  <pre><code>st_crs(shapefiles[[1]]) &lt;- 4326
  </code></pre>
  <p>If I try to use <code>lapply</code> or <code>purrr::map</code> using this code, then it does not work.</p>
  <pre><code>lapply(myfiles, st_crs() &lt;- 4326)
  </code></pre>
  <p><strong>Error</strong></p>
  <blockquote>
  <p>Error in st_crs() &lt;- 4326 : invalid (NULL) left side of assignment</p>
  </blockquote>
  <p>I also tried using <code>st_transform</code> in similar ways, but it also didn't work.</p>
  <p>What I want to do is to use something like <code>st_crs(shapefiles[[1]]) &lt;- 4326</code> but apply it to each element (i.e., shapefile) in the list. Also, I'd like to avoid using a for loop. I would rather use an <code>apply</code> or <code>purrr</code> function.</p>