分割数值yyyymmdd栏 [英] Split numeric yyyymmdd column
问题描述
我有一个包含DATE列的数据框,我将其称为NM_DATA $ DATE.我知道,它是yyyymmdd形式的数字,对此有一百个线程...
I have a dataframe containing a DATE column which I reference as NM_DATA$DATE. It is a numeric in the form of yyyymmdd, I know, a hundred threads on this...
我尝试了所有可以找到的解决方案,但它们对我不起作用.即将重新加载RStudio.
I tried every solution I could find and they won't work for me. About to reload RStudio.
NM_DATA$DATES <- as.Date(NM_DATA$DATES, "%Y%m%d")
返回
as.Date.default(NM_DATA $ DATES,%Y%m%d")中的错误:不知道如何将"NM_DATA $ DATES"转换为日期"类
Error in as.Date.default(NM_DATA$DATES, "%Y%m%d") : do not know how to convert 'NM_DATA$DATES' to class "Date"
有人可以解释一下如何获取DATE列:
Can someone please explain how to get DATE column:
DATE
19870401
19870501
etc
进入:
DATE
1987-04
1987-05
任何帮助将不胜感激!
推荐答案
为了在这里有完整的答案,我在某种程度上充实@Alex的答案.
In the interest of having a complete answer here, I'm fleshing out @Alex's answer somewhat.
as.Date
函数可用于将字符串(或其向量)转换为 Date
格式. strptime
(?strptime
)的帮助页面提供了一些有关 Date
对象可以使用的可能格式的有价值的信息.
The as.Date
function can be used to convert a character string (or vector thereof) to Date
format. The help page for strptime
(?strptime
) gives some valuable information about possible formats that Date
objects can use.
在您的情况下,您要将 NM_DATA $ DATE
向量转换为日期.格式yyyymmdd由%Y%m%d
表示,因此,如果您的向量是字符,我们可以像这样转换它:
In your case, you want to convert the NM_DATA$DATE
vector to dates. The format yyyymmdd is represented by %Y%m%d
, thus if your vector is character, we can convert it like so:
NM_DATA$DATE <- as.Date(NM_DATA$DATE, format='%Y%m%d')
但是,如果它是数字(而不是字符),我们首先需要强制转换为字符,以避免必须提供'origin'
错误.(您可以使用 mode(NM_DATA $ DATE)
检查向量的模式.)
However, if it is numeric (instead of character), we first need to coerce to character to avoid an 'origin' must be supplied
error. (You could check the mode of the vector with mode(NM_DATA$DATE)
.)
NM_DATA$DATE <- as.Date(as.character(NM_DATA$DATE), format='%Y%m%d')
现在向量是一个 Date
对象,我们可以采用多种方式对其进行格式化(在?strptime
中概述).要提取年,月和日的数字,请执行以下操作:
Now that the vector is a Date
object, we can format it in various ways (outlined at ?strptime
). To extract year, month and day numbers:
NM_DATA$YEAR <- format(NM_DATA$DATE, '%Y')
NM_DATA$MONTH <- format(NM_DATA$DATE, '%m')
NM_DATA$DAY <- format(NM_DATA$DATE, '%d')
如果您想要月份名称,则可以使用%B
(或%b
(表示缩写的月份名称)),例如:
If you want month name, instead, you can use %B
(or %b
, for abbreviated month names), e.g.:
NM_DATA$MONTHNAME <- format(NM_DATA$DATE, '%B')
这篇关于分割数值yyyymmdd栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!