简体   繁体   English

如何从R中的不同日期格式获取月/年/星期几

[英]How to get month/year/day of the week from different date formats in R

I have a data frame that contains a column call "date".我有一个包含列调用“日期”的数据框。 However the date formats are distinctively different.然而,日期格式明显不同。 Data type is string.数据类型为字符串。 I am trying to create "month" "year" and "day of the week" columns from this data column.我正在尝试从此数据列创建“月”、“年”和“星期几”列。

dataid     date
1         Tue 11/3
2         Wed 11/4 
3          N/A
4         Monday, February 1, 2016
5         Thursday, March 25, 2015 

What is the best way to do this?做这个的最好方式是什么?

The robust way is to use lubridate::parse_date_time() , but those dates witout year may be wrongly parsed (you may need to manually edit it).可靠的方法是使用lubridate::parse_date_time() ,但那些没有年份的日期可能会被错误解析(您可能需要手动编辑它)。

You may read "help("strptime")" to learn more about how to format orders to parse your date.您可以阅读“help("strptime")”以了解有关如何格式化订单以解析您的日期的更多信息。

ps March 25, 2015 is wednesday, not Thursday as in your example data. ps 2015 年 3 月 25 日是星期三,而不是示例数据中的星期四。

library(dplyr)

library(lubridate)


df <- data.table::fread(
"dataid     date
1         'Tue 11/3'
2         'Wed 11/4' 
3         'N/A'
4         'Monday, February 1, 2016'
5         'Thursday, March 25, 2015'
",quote="\'")

df.new <- df %>%
  mutate(
    date2 =lubridate::parse_date_time(x =date, orders = c("%a %m/%d", "%A, %B %d, %Y"))
  )
#> Warning: 1 failed to parse.


df.new
#>   dataid                     date      date2
#> 1      1                 Tue 11/3 2018-11-03
#> 2      2                 Wed 11/4 2018-11-04
#> 3      3                      N/A       <NA>
#> 4      4 Monday, February 1, 2016 2016-02-01
#> 5      5 Thursday, March 25, 2015 2015-03-25

Created on 2018-10-08 by the reprex package (v0.2.1)reprex 包(v0.2.1) 于 2018 年 10 月 8 日创建

from there you can extract year, month, day of week like this:从那里你可以像这样提取年、月、星期几:

df.new %>%
  mutate(
    year = lubridate::year(date2),
    month = lubridate::month(date2),
    day_of_week = weekdays(date2)
  )

  #  dataid                     date      date2 year month day_of_week
  #1      1                 Tue 11/3 2018-11-03 2018    11    Saturday
  #2      2                 Wed 11/4 2018-11-04 2018    11      Sunday
  #3      3                      N/A       <NA>   NA    NA        <NA>
  #4      4 Monday, February 1, 2016 2016-02-01 2016     2      Monday
  #5      5 Thursday, March 25, 2015 2015-03-25 2015     3   Wednesday

If the day and month are written as characters, then regular expressions can be used within a dplyr::case_when() call:如果将日期和月份写为字符,则可以在dplyr::case_when()调用中使用正则表达式:

library(dplyr)

df <- df %>%
  mutate(
    day_of_the_week = case_when(
      grepl("mon", date, ignore.case = T) ~ "mon",
      grepl("tue", date, ignore.case = T) ~ "tues",
      grepl("wed", date, ignore.case = T) ~ "wed",
      grepl("thu", date, ignore.case = T) ~ "thurs",
      grepl("fri", date, ignore.case = T) ~ "fri",
      grepl("sat", date, ignore.case = T) ~ "sat",
      grepl("sun", date, ignore.case = T) ~ "sun",
      T ~ NA_character_
    ),
    month = case_when(
      grepl("jan", date, ignore.case = T) ~ "jan",
      grepl("feb", date, ignore.case = T) ~ "feb",
      grepl("mar", date, ignore.case = T) ~ "mar",
      grepl("apr", date, ignore.case = T) ~ "apr",
      grepl("may", date, ignore.case = T) ~ "may",
      grepl("jun", date, ignore.case = T) ~ "jun",
      grepl("jul", date, ignore.case = T) ~ "jul",
      grepl("aug", date, ignore.case = T) ~ "aug",
      grepl("sep", date, ignore.case = T) ~ "sep",
      grepl("oct", date, ignore.case = T) ~ "oct",
      grepl("nov", date, ignore.case = T) ~ "nov",
      grepl("dec", date, ignore.case = T) ~ "dec",
      T ~ NA_character_
    )
  )

#   dataid                     date day_of_the_week month
# 1      1                 Tue 11/3            tues  <NA>
# 2      2                 Wed 11/4             wed  <NA>
# 3      3                     <NA>            <NA>  <NA>
# 4      4 Monday, February 1, 2016             mon   feb
# 5      5 Thursday, March 25, 2015           thurs   mar

It's a harder to pull out day/month number (you could possibly do it in a similar way for days of month between 13 and 31, but otherwise it's impossible to know if the number is for the day or month).提取日/月数比较困难(您可以在 13 到 31 之间的月份中以类似的方式执行此操作,否则就不可能知道该数字是当天还是月份)。

Data数据

df <- read.table(text = "
dataid     date
1         'Tue 11/3'
2         'Wed 11/4'
3         N/A
4         'Monday, February 1, 2016'
5         'Thursday, March 25, 2015'",
                 header = T,
                 stringsAsFactors = F,
                 na.strings = "N/A")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM