I have a data frame that looks like this:
ID lat long score
1 41.5 -62.3 22.4
2 41.0 -70.2 21.9
3 42.2 -63.0 22.7
4 36.7 -72.9 20.0
5 36.2 -62.4 24.1
6 35.8 -61.7 24.7
7 40.8 -61.9 22.1
I would like to identify rows of this data frame for which values for lat are within 1 unit of each other AND values for long are within 1 unit of each other AND values for score are within 0.7 units of each other. To indicate which rows meet these conditions, I'd like to add a new column (ID.matches) that provides values for ID for rows meeting the above conditions. The final data frame could look something like this:
ID lat long score ID.matches
1 41.5 -62.3 22.4 3, 7
2 41.0 -70.2 21.9 0
3 42.2 -63.0 22.7 1
4 36.7 -72.9 20.0 0
5 36.2 -62.4 24.1 6
6 35.8 -61.7 24.7 5
7 40.8 -61.9 22.1 1
I'm not really sure where to start with this...I think some sort of conditional function using dplyr or sapply? I'm also not sure if another data structure should be used for ID.matches, since some rows will have more than one match.
Thanks for any help!