2015-10-18 47 views
2

我的任務是從所有30個團隊獲取棒球數據並將其組合到一張表中。但是,我不斷收到整數(0)作爲回報。這裏是我的數據幀:使用rvest後合併數據幀

install.packages("rvest") 
library(rvest) 

# Store web url 
baseball1 <- read_html("http://www.baseball-reference.com/teams/ARI/") 

#Scrape the website for the franchise table 
franch1 <- baseball1 %>% 
    html_nodes("#franchise_years") %>% 
    html_table() 
franch1 

# Store web url 
baseball2 <- read_html("http://www.baseball-reference.com/teams/ATL/") 

#Scrape the website for the franchise table 
franch2 <- baseball2 %>% 
    html_nodes("#franchise_years") %>% 
    html_table() 
franch2 

這是該數據幀的結構:STR(franch1)

List of 1 
$ :'data.frame': 18 obs. of 21 variables: 
    ..$ Rk  : int [1:18] 1 2 3 4 5 6 7 8 9 10 ... 
    ..$ Year  : int [1:18] 2015 2014 2013 2012 2011 2010 2009 2008 2007 2006 ... 
    ..$ Tm  : chr [1:18] "Arizona Diamondbacks" "Arizona Diamondbacks" "Arizona Diamondbacks" "Arizona Diamondbacks" ... 
    ..$ Lg  : chr [1:18] "NL West" "NL West" "NL West" "NL West" ... 
    ..$ G   : int [1:18] 162 162 162 162 162 162 162 162 162 162 ... 
    ..$ W   : int [1:18] 79 64 81 81 94 65 70 82 90 76 ... 
    ..$ L   : int [1:18] 83 98 81 81 68 97 92 80 72 86 ... 
    ..$ Ties  : int [1:18] 0 0 0 0 0 0 0 0 0 0 ... 
    ..$ W-L%  : num [1:18] 0.488 0.395 0.5 0.5 0.58 0.401 0.432 0.506 0.556 0.469 ... 
    ..$ pythW-L% : num [1:18] 0.504 0.415 0.493 0.53 0.545 0.428 0.462 0.509 0.487 0.491 ... 
    ..$ Finish : chr [1:18] "3rd of 5" "5th of 5" "2nd of 5" "3rd of 5" ... 
    ..$ GB  : chr [1:18] "13.0" "30.0" "11.0" "13.0" ... 
    ..$ Playoffs : chr [1:18] "" "" "" "" ... 
    ..$ R   : int [1:18] 720 615 685 734 731 713 720 720 712 773 ... 
    ..$ RA  : int [1:18] 713 742 695 688 662 836 782 706 732 788 ... 
    ..$ BatAge : num [1:18] 26.6 27.6 28.1 28.3 28.2 26.8 26.5 26.7 26.6 29.6 ... 
    ..$ PAge  : num [1:18] 27.1 28 27.6 27.4 27.4 27.9 27.7 29.4 28.2 28.8 ... 
    ..$ #Bat  : int [1:18] 50 52 44 48 51 48 45 41 47 45 ... 
    ..$ #P  : int [1:18] 27 25 23 23 25 28 24 20 26 25 ... 
    ..$ Top Player: chr [1:18] "P.Goldschmidt (8.8)" "P.Goldschmidt (4.5)" "P.Goldschmidt (7.1)" "A.Hill (5.0)" ... 
    ..$ Managers : chr [1:18] "C.Hale (79-83)" "K.Gibson (63-96) and A.Trammell (1-2)" "K.Gibson (81-81)" "K.Gibson (81-81)" ... 

什麼功能做我使用這些數據幀結合?您的幫助非常感謝,並告訴我是否需要提供其他信息。

回答

0

這是因爲您的特許經營表被列爲需要轉換爲數據幀的數據幀值。此外,「read_html」不適用於我,我使用「html」來代替。

試試這個:

# Store web url using "html" not "read_html" 
baseball1 <- html("http://www.baseball-reference.com/teams/ARI/") 

#Scrape the website for the franchise table 
franch1 <- baseball1 %>% 
    html_nodes("#franchise_years") %>% 
    html_table() 
franch1 

# Store web url 
baseball2 <- html("http://www.baseball-reference.com/teams/ATL/") 

#Scrape the website for the franchise table 
franch2 <- baseball2 %>% 
    html_nodes("#franchise_years") %>% 
    html_table() 
franch2 

franch1 <- as.data.frame(franch1) 
franch2 <- as.data.frame(franch2) 

franchMerged <- rbind(franch1, franch2) 

讓我知道這是否爲你的作品。