title: r - Create linestring from two points in same row in dataframe
body: <p>I was wondering if there is a way to create <code>linestring</code> from two points given in the same row in a dataframe in a new geometry column. In other words longitudes and latitudes of the two points are given in a dataframe like the following:</p>
  
  <pre><code>df &lt;- data.frame(id = c("a", "b"), lon1 = c(1,2), lat1 = c(3,4), lon2 = c(5,6), lat2 = c(7,8))  
  </code></pre>
  
  <p>where <code>lon1</code> and <code>lat1</code> represent the coordinates of the first point and <code>lon2</code> and <code>lat2</code> are the coordinates of the second point. The desired dataframe would have two rows and two columns - the <code>id</code> column and a <code>geometry</code> column.</p>
  
  <p>I tried with <code>sf::st_linestring</code> but seems this function only works with matrices. </p>
  
  <p>Desired dataframe:</p>
  
  <pre><code>desired_df &lt;- data.frame(id = c("a", "a", "b", "b"), lon = c(1,2,5,6), lat = c(3,4,7,8)) %&gt;% st_as_sf(coords = c("lon", "lat"), dim = "XY") %&gt;% st_set_crs(4236) %&gt;% group_by(id) %&gt;% summarise(geometry = st_union(geometry), do_union = FALSE) %&gt;% st_cast("LINESTRING")
  </code></pre>