title: Use apply() with a simple features (SF) function
body: <p>I've written a function to calculate the maximum distance between a centroid and the edge of its polygon, but I can't figure out how to run it on each individual polygon of a simple features ("sf) data.frame.</p>
  
  <pre><code>library(sf)
  
  distance.func &lt;- function(polygon){
    max(st_distance(st_cast(polygon, "POINT"), st_centroid(polygon)))
  }
  </code></pre>
  
  <p>If I test the function on a single polygon it works. (The warning messages are irrelevant to the current issue).</p>
  
  <pre><code>nc &lt;- st_read(system.file("shape/nc.shp", package="sf")) # built in w/package
  nc.1row &lt;- nc[c(1),] # Just keep the first polygon
  
  &gt;distance.func(nc.1row)
  24309.07 m
  Warning messages:
  1: In st_cast.sf(polygon, "POINT") :
     repeating attributes for all sub-geometries for which they may not be constant
  2: In st_centroid.sfc(st_geometry(x), of_largest_polygon = of_largest_polygon) :
     st_centroid does not give correct centroids for longitude/latitude data
  </code></pre>
  
  <p>The problem is applying this function to the entire data.frame.</p>
  
  <pre><code>nc$distance &lt;- apply(nc, 1, distance.func)
  Error in UseMethod("st_cast") :
   no applicable method for 'st_cast' applied to an object of class "list"
  </code></pre>
  
  <p>What can I do to run this function (or one like it) for each individual polygon in an object of class "sf"?</p>