2013-11-28 47 views
0

轉換刮的R數據我試圖刮這個網站http://www.hockeyfights.com/fightlog/,但很難把它放入一個很好的數據框。到目前爲止,我有這個:使用readHTMLTable()

> asdf <- htmlParse("http://www.hockeyfights.com/fightlog/1") 
> asdf.asdf <- readHTMLTable(asdf) 

然後我得到這個巨大的名單。我如何將它轉換爲只有玩家名稱(誰在戰鬥中)有n行(戰鬥次數)的2列數據框?

感謝您的幫助提前。

+1

目前尚不清楚你在找什麼。你能顯示預期輸出的一些行嗎? – agstudy

回答

0

這是你輸出的結果嗎?

require(RCurl); require(XML) 
asdf <- htmlParse("http://www.hockeyfights.com/fightlog/1") 
asdf.asdf <- readHTMLTable(asdf) 

首先,請各位玩家的表,他們一直在戰鬥的個性化......

# get variable with player names 
one <- as.character(na.omit(asdf.asdf[[1]]$V3)) 
# get counts of how many times each name appears 
two <- data.frame(table(one)) 
# remove non-name data 
three <- two[two$one != 'Away/Home Player',] 
# check 
head(three) 
one Freq 
1 Aaron Volpatti 1 
3 Brandon Bollig 1 
4  Brian Boyle 1 
5 Brian McGrattan 1 
6  Chris Neil 2 
7 Colin Greening 1 

二,製作的是誰在打每一個表...

# make data frame of pairs by subsetting the vector of names 
four <- data.frame(away = one[seq(2, length(one), 3)], 
        home = one[seq(3, length(one), 3)]) 
# check 
head(four) 
      away   home 
1 Brian Boyle  Zdeno Chara 
2 Tom Sestito  Chris Neil 
3  Dale Weise Mark Borowiecki 
4 Brandon Bollig Brian McGrattan 
5 Scott Hartnell  Eric Brewer 
6 Colin Greening Aaron Volpatti 
+0

是的。但我可以再問一件事嗎?你會如何將名字放在其他列中?所以,如果亞倫Volpatti和布蘭登Bollig在戰鬥,Volpatti將col1,Bollig將col2。隨後是col1的Dillon,col2的McGrattan。謝謝您的幫助! – user1769069

+0

@ user1769069完成。如果這回答您的問題,請通過[接受答案](http://meta.stackexchange.com/a/5235)表明。 – Ben

相關問題