1

I am an R beginner looking to add a new variable to a long dataset. I currently have a long dataset with three variables--ID; latitude; longitude. I'd like to add a fourth variable that identifies the location within each group. There are a different number of locations within each group.

For example, this is what my data currently look like:

ID  lat     lon  
100 33.737  -84.433  
100 33.751  -84.486  
101 33.681  -84.402  
102 33.756  -84.414  
102 33.751  -84.486  
102 33.730  -84.446  
103 33.751  -84.486  
103 33.724  -84.499  
104 33.737  -84.433  
104 33.799  -84.195  
105 33.678  -84.417  
105 33.661  -84.448  
106 33.728  -84.508  
107 33.852  -84.359  
107 33.756  -84.414  
107 33.737  -84.433  

And here is what I would like the data to look like:

   ID      lat     lon      location_number  
100 33.737  -84.433  1   
100 33.751  -84.486  2  
101 33.681  -84.402  1  
102 33.756  -84.414  1   
102 33.751  -84.486  2   
102 33.730  -84.446  3   
103 33.751  -84.486  1  
103 33.724  -84.499  2   
104 33.737  -84.433  1  
104 33.799  -84.195  2  
105 33.678  -84.417  1  
105 33.661  -84.448  2  
106 33.728  -84.508  1  
107 33.852  -84.359  1  
107 33.756  -84.414  2  
107 33.737  -84.433  3  

Thank you so much for any assistance you are able to provide!

MrFlick
  • 195,160
  • 17
  • 277
  • 295
IGR
  • 31
  • 1
  • You might want to check out this post, which seems to address what you're looking for: https://stackoverflow.com/questions/12925063/numbering-rows-within-groups-in-a-data-frame – Ekholme Oct 15 '19 at 18:59
  • Thank you so much, this is exactly what I was looking for. – IGR Oct 16 '19 at 16:38

0 Answers0