Search code examples
rgroup-bymappingdplyrsummarize

Summarize number of unique rows in data frame r


Need your best advice. Trying to map bike routes in NY.

library(tidyverse)
bikes <- read.csv("August.csv", header = TRUE)
str(bikes) # 1557663 obs. of  15 variables
summary(bikes)
names(bikes)

That is how one route looks like

# Sample route (example)
route(from = "Clark St & Henry St, New York, NY", to = "Queens Plaza North & 
Crescent St, New York, NY")
rt <- route(from = "Clark St & Henry St, New York, NY", to = "Queens Plaza 
North & Crescent St, New York, NY")
nyc <- qmap("New York, NY", color = 'bw', zoom = 12)  
nyc + geom_path(aes(x = rt$startLon, y = rt$startLat), 
            colour = "red", data = rt, alpha = 1, size = 0.2)

# How many stations are unique?
start.station <- bikes$start.station.name
unique(start.station) # 574 stations
end.station <- bikes$end.station.name
unique(end.station) # 582 stations

names(bikes)
# [1] "tripduration"            "starttime"               "stoptime"               
# [4] "start.station.id"        "start.station.name"      
# "start.station.latitude" 
# [7] "start.station.longitude" "end.station.id"          "end.station.name"       
# [10] "end.station.latitude"    "end.station.longitude"   "bikeid"                 
# [13] "usertype"                "birth.year"              "gender"  

I can assume that I need only two columns - for start and end stations names.

# eliminate all columns besides two - start and end stations
only.stations <- bikes %>% as_tibble() %>% 
mutate(tripduration = NULL, starttime = NULL, stoptime = NULL, 
start.station.id = NULL,
start.station.latitude = NULL, start.station.longitude = NULL, 
end.station.id = NULL,
end.station.latitude = NULL, end.station.longitude = NULL, bikeid = NULL, 
usertype = NULL, 
birth.year = NULL, gender = NULL)

only.stations # A tibble: 1,557,663, so, we have 1,557,663 rides
# start.station.name          end.station.name
# <fctr>                    <fctr>
#1               Avenue D & E 3 St            E 3 St & 1 Ave
#2              Broadway & E 14 St         E 7 St & Avenue A
#3  Metropolitan Ave & Bedford Ave       Union Ave & N 12 St
#4                 E 10 St & 5 Ave           E 10 St & 5 Ave
#5           LaGuardia Pl & W 3 St            E 3 St & 1 Ave
#6         Grand St & Havemeyer St Graham Ave & Conselyea St
#7           N 12 St & Bedford Ave  Bedford Ave & Nassau Ave
#8                 9 Ave & W 18 St     Pershing Square North
#9                  E 2 St & 2 Ave         E 2 St & Avenue C
#10   MacDougal St & Washington Sq        E 10 St & Avenue A
# ... with 1,557,653 more rows
# unique(only.stations) # A tibble: 129,839 × 2 - so, do we have 129,839 
unique (only.stations)
View(only.stations)

My question - how to group and summarize 129,839 unique rows and understand how frequently is each route used. I believe that it is with dplyr - group_by() and summarize(), but tried several options and nothing works. :(

Sincerely Oleksiy


Solution

  • It looks like your question is about counting the frequency of each unique row in only.stations. The keyword you're missing is n() inside dplyr's summarise function. Try:

    only.stations %>% 
        group_by(start.station.name, end.station.name) %>% 
        summarise(frequency = n())