使用rvest后合并数据帧

时间:2015-10-18 21:35:38

标签: r merge rvest

我的任务是从所有30支球队中获取棒球数据并将其全部合并到一张桌子中。但是,我一直得到整数(0)作为回报。以下是我的数据框:

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)" ...

我使用什么功能来组合这些数据帧?非常感谢您的帮助,如果我需要提供其他信息,请告知我们。

1 个答案:

答案 0 :(得分:0)

这是因为您的特许经营权表被列为需要转换为数据框的数据框值。此外,&#34; read_html&#34;没有为我工作我使用&#34; html&#34;代替。

试试这个:

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

请告诉我这是否适合您。

相关问题