-2

我正在编写一个脚本来从棒球参考网页获取信息。我第一次编写代码时它运行良好,所有存储为因子的日期都被正确解析为使用 as.Date() 函数的日期。尽管如此,一天后,我运行了相同的脚本,并且在变量的某些日期中得到了“NA”,而其他日期则得到了很好的转换。还有另一个因素变量,其中所有变量都返回为“NA”。

我已经对此进行了研究,但我只能发现有关“NA”的问题,因为值上缺少天数(只有月份和年份)。

我也尝试将 sys.setlocale 从葡萄牙更改为美国 (LC_ALL","English") 但我得到了相同的结果。

我使用的脚本是。你有什么遗漏的提示吗?

谢谢。

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
4

1 回答 1

0

尝试安装和使用 package lubridate,对所有日期时间操作非常有用:

library(lubridate)
mdy(batting$Debut)
于 2016-05-13T20:18:42.850 回答