Search code examples
rdegrees

Operation between 2 columns (degree values)


In R I can't find the way to calculate in degrees for an Aspect.

I have a shapefile with 2 columns for the aspect. The first column ASPECT correspond to the new aspect of the point and the second column Aspect is the reference aspect. I would like to keep only the values that are not more or less than 45° around the value in the ASPECT column. I want to do this calculation for each value.

My data looks like this :

ID   ASPECT   Aspect
0  17.15395 223.9521
1 323.72394 225.1147
2 294.01069 225.7785
3 302.72811 226.4539
4 321.79413 227.1748
5 326.36743 227.9040

I thought about doing the difference between ASPECT and Aspect in order to filter all of the values that does not fit. The problem is that as it is in degrees I have sometimes differences up to 359 which is actually only a few degrees.

    gr73999<-readOGR(dsn=wd, layer="73366_CORRPOINTS")
    gr<-as.data.frame(gr73999)
    gr_df<- gr[,c("ID","ASPECT","Aspect")]


    gr_df$degree_diff<- gr_df$Aspect-gr_df$ASPECT

ID    ASPECT   Aspect     Difference_aspect
0  17.15395 223.9521        -206.79816
1 323.72394 225.1147          98.60924
2 294.01069 225.7785          68.23220
3 302.72811 226.4539          76.27422
4 321.79413 227.1748          94.61931
5 326.36743 227.9040          98.46343    

    min(gr_df$degree_diff)
-359.0588
    max(gr_df$degree_diff)
358.2314

My question is : how can I do to have all of my calculation in degrees ? Is it possible to have -30 instead of 330 if Aspect is 340 and ASPECT is 10 ?


Solution

  • I think this does what you are looking for. It will always give an answer between -180 and +180.

    gr_df$degree_diff <- ((gr_df$Aspect-gr_df$ASPECT+180) %% 360) - 180