Sidoti, Salvatore A.
2015-Dec-17 21:25 UTC
[R] Converting from Continuous 2D Points to Continuous 2D Vectors
Greetings!
I have a fairly large dataframe (df) with pathing information in the form of
continuous x,y coordinates:
df$x
df$y
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))
angle(t(x),y)
[1] 36.8699
Thank you!
Boris Steipe
2015-Dec-18 02:59 UTC
[R] Converting from Continuous 2D Points to Continuous 2D Vectors
Ok. And what is the problem now? B. On Dec 17, 2015, at 4:25 PM, Sidoti, Salvatore A. <sidoti.23 at buckeyemail.osu.edu> wrote:> Greetings! > > I have a fairly large dataframe (df) with pathing information in the form of continuous x,y coordinates: > > df$x > df$y > > 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)) > angle(t(x),y) > [1] 36.8699 > > Thank you! > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
David L Carlson
2015-Dec-18 17:23 UTC
[R] Converting from Continuous 2D Points to Continuous 2D Vectors
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.
https://cran.r-project.org/web/views/SpatioTemporal.html
-------------------------------------
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
Greetings!
I have a fairly large dataframe (df) with pathing information in the form of
continuous x,y coordinates:
df$x
df$y
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))
angle(t(x),y)
[1] 36.8699
Thank you!
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.