将字符转换为类Date [英] Convert character to class Date
问题描述
我有一个带有日期字符列的数据框.
I have a data frame with a character column of dates.
当我使用 as.Date
时,除少数实例外,大多数日期都可以正确解析.下面的示例有望向您显示正在发生的事情.
When I use as.Date
, most of my dates are parsed correctly, except for a few instances. The example below will hopefully show you what is going on.
# my attempt to parse the string to Date -- uses the stringr package
prods.all$Date2 <- as.Date(str_sub(prods.all$Date, 1,
str_locate(prods.all$Date, " ")[1]-1),
"%m/%d/%Y")
# grab two rows to highlight my issue
temp <- prods.all[c(1925:1926), c(1,8)]
temp
# Date Date2
# 1925 10/9/2009 0:00:00 2009-10-09
# 1926 10/15/2009 0:00:00 0200-10-15
如您所见,某些日期的年份不正确.当日期为两位数时,似乎会出现这种模式.
As you can see, the year of some of the dates is inaccurate. The pattern seems to occur when the day is double digit.
我们将不胜感激.
推荐答案
您可能使事情复杂化了,请问有什么理由需要Stringer软件包吗?您可以使用 as.Date
及其参数 format
指定字符串的 input 格式.
You may be overcomplicating things, is there any reason you need the stringr package? You can use as.Date
and its format
argument to specify the input format of your string.
df <- data.frame(Date = c("10/9/2009 0:00:00", "10/15/2009 0:00:00"))
as.Date(df$Date, format = "%m/%d/%Y %H:%M:%S")
# [1] "2009-10-09" "2009-10-15"
注意?日期
的详细信息部分:
将根据指定格式对字符字符串进行必要的处理:忽略任何尾随字符
Character strings are processed as far as necessary for the format specified: any trailing characters are ignored
因此,这也起作用:
as.Date(df$Date, format = "%m/%d/%Y)
# [1] "2009-10-09" "2009-10-15"
可用于指定输入格式
的所有转换规范可在?strptime
的 Details 部分中找到.确保转换规范的 order 以及任何分隔符与您的 input 字符串的格式完全对应.
All the conversion specifications that can be used to specify the input format
are found in the Details section in ?strptime
. Make sure that the order of the conversion specification as well as any separators correspond exactly with the format of your input string.
更一般而言,如果您还需要时间部分,请使用 as.POSIXct
或 strptime
:
More generally and if you need the time component as well, use as.POSIXct
or strptime
:
as.POSIXct(df$Date, "%m/%d/%Y %H:%M:%S")
strptime(df$Date, "%m/%d/%Y %H:%M:%S")
我在猜测从您提供的部分结果来看您的实际数据会是什么.
I'm guessing at what your actual data might look at from the partial results you give.
这篇关于将字符转换为类Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!