as.date在R data.frame中收到了NA

时间:2017-06-04 13:55:20

标签: r date strptime as.date

请帮助我将<form name="form" class="form-horizontal" novalidate> <div class="form-group"> <label> Password: </label> <input name="password" type="password" placeholder="Password" required ng-model="password" ng-pattern="passwordRegex" /> </div> <div ng-show="form.$submitted && form.password.$invalid"> <div ng-show="form.password.$error.required"> RequiredField</div> <div ng-show="form.password.$error.pattern"> InvalidPassword</div> </div> <div class="form-group"> <label>ConfirmPassword:</label> <input name="confirmPassword" type="password" placeholder="ConfirmPassword" required ng-model="confirmPassword" ng-match="password" /> </div> <div ng-show="password != confirmPassword"> <div ng-show="form.confirmPassword.$error.match"> PasswordMismatch</div> </div> <div ng-show="form.$submitted && form.confirmPassword.$invalid"> <div ng-show="form.confirmPassword.$error.required"> RequiredField</div> </div> <button type="submit" ng-click="!form.$invalid && testCtrl.ok()">Ok</button> </form>转换为characters格式

dateas.date没有工作......

strptime

然后我尝试使用代码:

c <- data.frame(id = c('1', '2', '3', '4'), Date = c("01-Feb-16","03-May-15","24-Oct-14","20-Oct-12"))
c$Date <- as.character(c$Date)
str(c)

c$Date <- as.Date(c$Date, format = "%d-%b-%y")

我已收到c$Date <- strptime(c$Date, '%d-%b-%y')

1 个答案:

答案 0 :(得分:1)

您的日期采用德语格式,包含英文月份名称。在您的代码之前尝试此操作:

c <- data.frame(lapply(c, function(y) {
y <- gsub("May", "Mai", y); y
y <- gsub("Oct", "Okt", y); y
}))

c$Date1 <- as.Date(c$Date, format = "%d-%b-%y")
c$Date2 <- strptime(c$Date, '%d-%b-%y')
c

# id      Date      Date1      Date2
# 1  1 01-Feb-16 2016-02-01 2016-02-01
# 2  2 03-Mai-15 2015-05-03 2015-05-03
# 3  3 24-Okt-14 2014-10-24 2014-10-24
# 4  4 20-Okt-12 2012-10-20 2012-10-20