我有一个字符串向量:
str <- c("01-", "01-just researching", "01-1-3 months", "01-immediately", "01-4-6 months", "01-more than 12 months", "01-7-12 months")
如果我使用来自parse_date_time
包的lubridate
解析它,如果我只解析前6个字符串,它会得到不同的结果。为什么呢?
parse_date_time(str, "dmy")
[1] NA NA "2003-01-01 UTC" NA "2006-04-01 UTC"
[6] NA "2012-07-01 UTC"
parse_date_time(str[1:6], "dmy")
[1] NA NA NA NA NA NA
答案 0 :(得分:5)
有一个函数guess_formats
可以解释哪些元素与模板匹配。正如您所看到的,只有最后一个字符串可以:
guess_formats(str, "dmy", print_matches = TRUE)
# dmy
# [1,] "01-" ""
# [2,] "01-just researching" ""
# [3,] "01-1-3 months" ""
# [4,] "01-immediately" ""
# [5,] "01-4-6 months" ""
# [6,] "01-more than 12 months" ""
# [7,] "01-7-12 months" "%d-%m-%y months"
我认为其余的是显而易见的。