2016-07-21 7 views
0

にスコアのテキストデータを引っ張る:Rに次のデータを取得する方法を把握しようとR

http://masseyratings.com/scores.php?s=285971&sub=14342&all=1&mode=3&sch=on&format=0

このことは、ほぼだけで動作しますが、私は上下にジャンクを排除したい、とスコアを取得します。

read.fwf('http://masseyratings.com/scores.php?s=285971&sub=14342&all=1&mode=3&sch=on&format=0', 
     widths=c(11,26,3,26,3,4,21), 
     skip = 8) 
+0

トップとボトムのジャンクはどういう意味ですか? –

答えて

0

まずはスタック交換を歓迎します!だから、私はあなたのコードで何かを変更しました。あなたは6つの幅しか必要としませんでした。私がオンラインからのデータを取り込んでいたとき、最初の行がかなり奇妙だったことに気がついたので、私はそれをすべて一緒に乗り越えて、その後手動で追加しました。

data <- read.fwf('http://masseyratings.com/scores.php?s=285971&sub=14342&all=1&mode=3&sch=on&format=0',widths=c(10,26,3,26,3,4), sep = "\t", header = FALSE, skip = 8) 
# This line subsets the data so you don't have that "junk" at the bottom and deletes the row 
# with the html tagging. 

data <- data[2:2424,] 
data <- data.frame(data) 

# Create a vector that has the column headers 
names <- c("date", "Team1","Runs", "Team 2","Runs","Something") 
colnames(data) <- names 

# Create the first row of data that we previously deleted. 

firstrow = data.frame("2016-04-03", "@Pirates", 4, "Cardinals",1,"") 
colnames(firstrow) <- names 

finaldata <- rbind.data.frame(firstrow,data) 

は、将来の参考のためにあなたがあなたの質問のお手伝いをしようとする人のために役立つだろうジャンクとして考えるもののスクリーンショットを投稿することができます。

UPDATE

data <- read.fwf('http://masseyratings.com/scores.php?s=285971&sub=14342&all=1&mode=3&sch=on&format=0', 
        widths=c(10,26,3,26,3,4), sep = "\t", header = FALSE, skip = 9) 

data <- data.frame(data) 

# This line subsets the data so you don't have that "junk" at the bottom and deletes the row 
# with the html tagging. 

firstrow <- read.fwf('http://masseyratings.com/scores.php?s=285971&sub=14342&all=1&mode=3&sch=on&format=0', 
        widths=c(-8,-1,-1,9,26,3,26,3,4), sep = "\t", header = FALSE, n = 1, skip = 8) 
firstrow <- data.frame(firstrow,stringsAsFactors=FALSE) 

firstrow[,1] <- paste("2",firstrow[1,1],sep = "") 

# Create a vector that has the column headers 
names <- c("date", "Team1","Runs", "Team 2","Runs","Something") 
colnames(data) <- names 



colnames(firstrow) <- names 

finaldata <- rbind.data.frame(firstrow,data) 

移動に列の負の値のデータの最初の行に欠けていたことすべてが「2になるように、それが働いたまで、私はそれで遊ん、上"次に、 "2"を貼り付け、rbind関数を使用して完全なデータフレームを作成します。それがあなたを助けることを願っています。

私はこのページでも同様にテストしました:http://masseyratings.com/scores.php?s=285971&sub=14342&all=1&mode=2&sch=on&format=0 期待どおりに動作しました。

+1

さん、ありがとうございます。複数の季節などのデータを取得したい場合、最初の行を手動で追加する必要はありませんか? – Fermat

+0

@Fermatアップデートをご覧ください。私はそれが最善の解決策ではないが、それはあなたの質問に答える必要があります知っている。これがうまくいく場合は、チェックをクリックしてください。 –

+0

@Fermatはフィードバックを得て、この解決策があなたを助けたかどうかを知りたいと思います。 –

関連する問題