I have an artificial data set that I created:
x<-rnorm(100,10,10)
y<-rnorm(100,20,10)
Location<-c((rep("AB", 40)),(rep("TA", 30)),(rep("OP", 30)))
Year<-c((rep("1999", 10)),(rep("2000", 9)),(rep("2001", 12)),(rep("2002", 9)),(rep("1999", 7)),(rep("2000", 6)),(rep("2001", 6)),(rep("2002", 11)),(rep("1999", 12)),(rep("2000", 8)),(rep("2001", 5)),(rep("2002", 5)))
Data<-cbind(x,y,Location,Year)
> head(Data)
x y Location Year
[1,] "1.8938661556415" "19.851256070398" "AB" "1999"
[2,] "21.0735971323312" "17.4993965352294" "AB" "1999"
[3,] "30.8347289164302" "7.63333686308105" "AB" "1999"
[4,] "8.913993138201" "14.7085296541221" "AB" "1999"
[5,] "20.8309225677419" "12.0888505284667" "AB" "1999"
[6,] "25.3978549194374" "20.47154776064" "AB" "1999"
I would like to take the arc2tan of each x and y such as:
Theta<-atan2(y[i+1]-y[i],x[i+1]-x[i])
but I only want to do this for each year within year location, meaning I do not want to find theta between 1999 and 2000, or between 2001 and 2002 etc. Only between x and y points of the same year in the same location.
I had originally written a loop that did the above (what I don't want to do), and I was wondering if anyone knew how to alter it, so that the loop would stop and reset itself for each year. The original loop is provided below:
for (i in 1:length(x)-1){
Theta[i]<-atan2(y[i+1]-y[i],x[i+1]-x[i])
}
Any helpers?