title: Retain start and end IDs when creating spatial lines from X Y points
body: <p>The following code will create SpatialLines that connect all x/y points in a dataset.  However, each of those x/y points has a unique ID.  I need to retain both the startpoint and the endpoint IDs of the x/y as "attributes" (the ArcGIS term) of each of the SpatialLines that connect them. </p>
  
  <p>Help would be appreciated. </p>
  
  <h1>I have a dataset of XY values that looks like this</h1>
  
  <pre><code>x&lt;-c(2,4,6,3,7,9,1)
  y&lt;-c(6,4,8,2,9,6,1)
  id&lt;-c("a","b","c","d","e","f","g")
  dataset&lt;-data.frame(cbind(x,y,id))
  dataset$x&lt;-as.numeric(as.character(dataset$x)) #converting from factor to numeric
  dataset$y&lt;-as.numeric(as.character(dataset$y))
  
  plot(dataset$x,dataset$y)
  </code></pre>
  
  <h1>Replicate the dataframe to cover for all possible combinations</h1>
  
  <pre><code>dataset&lt;-do.call(rbind, replicate(7, dataset, simplify=FALSE))
  </code></pre>
  
  <h1>Now, create a matrix with all the same destination points, mixed:</h1>
  
  <pre><code>nm=matrix(ncol=3)
  for (i in 1:7){
    nm&lt;-rbind(nm,do.call(rbind,replicate(7,as.matrix(dataset[i,]),simplify=FALSE)))
  }
  nm&lt;-nm[-1,]
  </code></pre>
  
  <h1>Rename the columns of matrix, so they make sense, and bind the existing data frame with the new matrix</h1>
  
  <pre><code>colnames(nm)&lt;-c("x2","y2","id.dest")
  newds&lt;-cbind(dataset,as.data.frame(nm))
  </code></pre>
  
  <h1>Remove duplicated trajectories:</h1>
  
  <pre><code>newds1&lt;-newds[-which(newds$id==newds$id.dest),]
  </code></pre>
  
  <h1>converting destination x &amp; y to numeric from factor</h1>
  
  <pre><code>newds1$x2&lt;-as.numeric(as.character(newds1$x2)) #converting from factor to numeric
  newds1$y2&lt;-as.numeric(as.character(newds1$y2))
  </code></pre>
  
  <h1>plotting the destination points . . .same as the origin points</h1>
  
  <pre><code>plot(newds1$x, newds1$y) 
  plot(newds1$x2, newds1$y2, col="red") 
  </code></pre>
  
  ####*
  
  <h1>converting the begin and end points to spatial lines</h1>
  
  <h2>raw list to store Lines objects</h2>
  
  <pre><code>l &lt;- vector("list", nrow(newds1)) #
  </code></pre>
  
  <h1>this l is now an empty vector w/ number of rows defined by length (nrow) of newds1</h1>
  
  <h1>splitting origin and destination coordinates so I can run this script</h1>
  
  <pre><code> origins&lt;-data.frame(cbind(newds1$x, newds1$y))
      destinations&lt;-data.frame(cbind(newds1$x2, newds1$y2))
  
      library(sp)
      for (i in seq_along(l)) {
        l[[i]] &lt;- Lines(list(Line(rbind(origins[i, ], destinations[i,]))), as.character(i))
      }
  
      l.spatial&lt;-SpatialLines(l)
      plot(l.spatial, add=T) 
  </code></pre>
  
  <p>The object newds1 contains both the startpoint and endpoint.  However, the final SpatialLines that are created from those start and endpoints (l.spatial) do not contain a reference to the start and endpoints.  I would like those SpatialLines to contain two "attribute" columns that refer to the IDs of the start and endpoints. I think this is a matter of binding newds1 (a dataframe) onto l.spatial (spatial lines), but the code I'm running doesn't seem to do it.  </p>
  
  <h1>Attempting to spatially bind the start and end IDs to l.spatial</h1>
  
  <pre><code>row.names(newds1)&lt;-1:length(newds1$id) #renaming rows in the dataframe so they match the spatial object
  
  id&lt;-newds1$id
  newds2&lt;-spCbind(l.spatial, id)
  </code></pre>
  
  <p>I get </p>
  
  <pre><code>"Error in (function (classes, fdef, mtable)  : 
    unable to find an inherited method for function ‘spCbind’ for signature ‘"SpatialLines", "factor"’"
  </code></pre>