1

How do you find the number of unique values when they span multiple columns. In my case, I am trying to get a count of the number of times each latitude/longitude occurs.

For example

+------------+
|  Lat Long  |
+------------+
| 39.3 -76.6 |
| 39.3 -76.6 |
| 39.3 -76.6 |
| 39.9 -76.1 |
+------------+

Would output

+---------------+
| Lat Long Freq |
+---------------+
| 39.3 -76.6 3  |
| 39.9 -76.1 1  |
+---------------+
cylus
  • 357
  • 1
  • 4
  • 14
  • 1
    Your title says spanning multiple rows, but your question says columns. Also, you said number of unique values, but aren't you trying to count the opposite, i.e. the number of times a value is repeated? – camille Jan 30 '20 at 17:57

3 Answers3

1

One option is to use count

library(dplyr) 
count(df1, Lat, Long)
akrun
  • 874,273
  • 37
  • 540
  • 662
1

dplyr::summarize() should also do the trick:

df <- df %>%
   group_by(Lat,Long) %>%
   summarize(Freq = n())
TTS
  • 1,818
  • 7
  • 16
1

A base R solution:

Data = data.frame(Lat = c(39.3, 39.3, 39.3, 39.9),
        Long = c(-76.6, -76.6, -76.6, -76.1))

aggregate(rep(1, nrow(Data)), Data, sum)

   Lat  Long x
1 39.3 -76.6 3
2 39.9 -76.1 1
G5W
  • 36,531
  • 10
  • 47
  • 80