使用 as.Date() 将日期转换为因子,某些结果会给出 "NA"s

using as.Date() to convert dates as factors give "NA"s for some results

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

我已经仔细查看了,但我只能找到关于 "NA"s 的问题,因为该值缺少天数(只有月份和年份)。

我也尝试将 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

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

library(lubridate)
mdy(batting$Debut)