Search code examples
rcategories

Assigning a special ID to similar latitude and longitude


I have a time-series dataset with latitude, longitude, and temprature data. I want to categorize the data by assigning a special ID to similar latitude and longitide values.

Sr.    Date       Longitude    Latitude    Temp      ID
1     1/1/1981       79          29.3       281       1
2     1/1/1981       78          29.3       271       2
3     1/1/1981       77          29.3       275       3
4     1/1/1981       76          29.3       282       4
5     2/1/1981       79          29.3       285       1
6     2/1/1981       78          29.3       283       2
7     2/1/1981       77          29.3       275       3
8     2/1/1981       76          29.3       283       4

i expect the data to have a special ID for similar locations.

Sr.    Date       Longitude    Latitude    Temp      ID
1     1/1/1981       79          29.3       281       1
5     2/1/1981       79          29.3       285       1

Solution

  • with dplyr:

    df %>%
        group_by(Longitude, Latitude) %>%
        mutate(id = cur_group_id()) %>%
        ungroup()
    
    

    Looking at your sample data and criteria that looks like it should do the job.