[R] Converting from Continuous 2D Points to Continuous 2D Vectors

David L Carlson dcarlson at tamu.edu
Fri Dec 18 18:23:27 CET 2015

Look at the CRAN Task View: "Handling and Analyzing Spatio-Temporal Data," particularly the section on "Moving objects, trajectories." The tools you need are probably already available.


David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Sidoti, Salvatore A.
Sent: Thursday, December 17, 2015 3:25 PM
To: r-help at r-project.org
Subject: [R] Converting from Continuous 2D Points to Continuous 2D Vectors


I have a fairly large dataframe (df) with pathing information in the form of continuous x,y coordinates:


With these data, I would like to:
1. Calculate a set of continuous vectors
2. Determine the angle between each of these vectors (in degrees)
3. Count the number of angles in the dataframe that meet a certain threshold (i.e. <90°)

Here's what I've come up with so far:

### Function that calculates the angle between two vectors in 2D space:

angle <- function(x,y){ # x and y are vectors
  dot.prod <- x%*%y 
  norm.x <- norm(x,type="2")
  norm.y <- norm(y,type="2")
  theta <- acos(dot.prod / (norm.x * norm.y))
  (180*as.numeric(theta))/pi # returns the angle in degrees

### Test the function:
x <- as.matrix(c(2,1))
y <- as.matrix(c(1,2))
[1] 36.8699

Thank you!
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list