2016-05-13 3 views
-2

Baseball Reference Webページから情報を取得するためのスクリプトを作成しています。 初めてコードを書いたときには正常に機能し、要因として格納されたすべての日付は、as.Date()関数を使って日付に正しく解析されました。 それにもかかわらず、1日後、私は同じスクリプトを実行しました。変数の一部の日付で「NA」を取得していて、他の変数はうまく変換されています。それらのすべてが「NA」として返される別の要素変数があります。as.Date()を使用して日付を要素に変換すると、結果として「NA」が返されます

私はそれについてゴーグルをしましたが、値の喪失日数(月と年のみ)のために "NA"に関する問題が見つかりませんでした。

私は(LC_ALL」、 『英語』)ポルトガルから米国へsys.setlocaleを変更することも試みたが、私は同じ結果を得る。私が使用

Thのスクリプトがある。あなたは、任意のヒントを持っていますか何が欠けている

おかげ

library(XML) 
Sys.setlocale("LC_ALL","English") # Used after first attempt 


# Web page with players 
url = "http://www.baseball-reference.com/bio/Venezuela_born.shtml" 

# Create a List of the data-frames found in the Web Page, and define the type of colum data 
url_Tables = readHTMLTable(url 
          ,stringAsFactors = FALSE 
          ,colClasses=c("integer","character",rep("integer",17) 
             ,rep("numeric", 4),"factor","factor" 
             , "character", "character") 
         ) 

# Assign First table of the Web Page to a Data.Frame 
batting = url_Tables[[1]] 

summary(batting) 

# Change the type of some colunms 
batting$Birthdate = as.Date(batting$Birthdate, "%b %d, %Y") # For this column some of the values are parsed OK and others not (NAs). 
batting$Debut = as.Date(batting$Debut, "%b %d, %Y")  # For this column all the values are converted as "NA"s 
+1

なぜ、 'Date'オブジェクトを' factor'sとして最初に表現していますか? – nrussell

+0

コードを再現できません。あなたの質問に 'dput(batting)'の結果を加えてみてください。 –

+0

私はコードを再現できますが、エラーは再現できません。私のシステムでは、すべての日付、 'Batting $ Birthdate'、' Batting $ Debut'は問題ありません。 「NA」値はない。 – RHertel

答えて

0

インストールしようとすると、使用パッケージlubridate、すべての日付時刻操作のために非常に有用:?。

library(lubridate) 
mdy(batting$Debut) 
関連する問題