2017-06-11 6 views
-1

ベストアドバイスが必要です。 NYの自転車ルートを地図に表示しようとしています。データフレーム内の一意の行数を要約してください。

つのルートが

# 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" 

どのように見えるか、私は2つの列のみを必要とすることを想定することができている

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

- 開始と終了ステーション名に。

# 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) 

私の質問 - 129,839のユニークな行をグループ化して要約する方法と、各ルートの使用頻度を理解する方法。私はそれがdplyr - group_by()とsummarize()であると信じていますが、いくつかのオプションを試してみると何も動作しません。 :(

敬具 オレクシ

答えて

1

あなたの質問がonly.stationsにそれぞれの固有の行の回数をカウントについてですように見えますが、不足しているしているキーワードがdplyrsummarise関数内n()で試してみてください。。

only.stations %>% 
    group_by(start.station.name, end.station.name) %>% 
    summarise(frequency = n()) 
関連する問題