【问题标题】:Converting factor into dates for boxplot in R unsuccessful with "st" "nd" "rd" "th" added to dates [duplicate]将因子转换为R中箱线图的日期不成功,将“st”“nd”“rd”“th”添加到日期[重复]
【发布时间】:2020-08-28 13:37:56
【问题描述】:

我拥有的数据集包含以下当前被识别为因素的日期列表:

Interview_Date = c("Monday 23rd May 2005", "Tuesday 24th May 2005", 
                   "Wednesday 25th May 2005", "Thursday 26th May 2005",
                   "Friday 27th May 2005", "Saturday 28th May 2005",
                   "Sunday 29th May 2005", "Monday 30th May 2005",
                   "Tuesday 31st May 2005", "Wednesday 1st June 2005",
                   "Thursday 2nd June 2005", "Friday 3rd June 2005",
                   "Saturday 4th June 2005", "Sunday 5th June 2005")

我无法将它们转换为日期。当我尝试时

as.Date(dataframe$Interview_Date, format = "%A%d%B%Y")

结果以“NA”结尾。我需要将其识别为日期,以便创建显示箱线图:

boxplot(EU_Opinion ~ Interview_Date,
    data = dataframe,
    xlab = "Date",
    ylab = "EU Opinion") 

但它目前不起作用,因为它是一个因子变量。我该怎么办?还是有其他方法可以创建箱线图?

【问题讨论】:

    标签: r boxplot as.date


    【解决方案1】:

    使用lubridate

    library(tidyverse)
    library(lubridate)
    df <- data.frame(Interview_Date = c("Monday 23rd May 2005", "Tuesday 24th May 2005", "Wednesday 25th May 2005", "Thursday 26th May 2005", "Friday 27th May 2005", "Saturday 28th May 2005","Sunday 29th May 2005", "Monday 30th May 2005", "Tuesday 31st May 2005", "Wednesday 1st June 2005", "Thursday 2nd June 2005", "Friday 3rd June 2005", "Saturday 4th June 2005", "Sunday 5th June 2005"))
    df <- df %>% 
      mutate(new_interview_Date = dmy(Interview_Date))
    glimpse(df)
    # Rows: 14
    # Columns: 2
    # $ Interview_Date     <fct> Monday 23rd May 2005, Tuesday 24th May 2005, Wednesday 25th ...
    # $ new_interview_Date <date> 2005-05-23, 2005-05-24, 2005-05-25, 2005-05-26, 2005-05-27,...
    

    【讨论】:

      【解决方案2】:

      您可以使用gsub 和正则表达式。

      as.Date(gsub("(.*\\d)\\D{1,2}(.*)", "\\1\\2", x), format="%A %e %B %Y")
      # [1] "2005-05-23" "2005-05-24" "2005-05-25" "2005-05-26" "2005-05-27" "2005-05-28"
      # [7] "2005-05-29" "2005-05-30" "2005-05-31" "2005-06-01" "2005-06-02" "2005-06-03"
      # [13] "2005-06-04" "2005-06-05"
      

      数据:

      x <- c("Monday 23rd May 2005", "Tuesday 24th May 2005", "Wednesday 25th May 2005", "Thursday 26th May 2005", "Friday 27th May 2005", "Saturday 28th May 2005","Sunday 29th May 2005", "Monday 30th May 2005", "Tuesday 31st May 2005", "Wednesday 1st June 2005", "Thursday 2nd June 2005", "Friday 3rd June 2005", "Saturday 4th June 2005", "Sunday 5th June 2005")
      

      【讨论】:

        【解决方案3】:

        您可以删除序数部分(即 st、nd、rd、th),然后转换为 Date 对象。

        as.Date(sub("(?<=\\d)\\D+?\\b", "", x, perl = TRUE), "%A %d %B %Y")
        
        # [1] "2005-05-23" "2005-05-24" "2005-05-25" "2005-05-26" "2005-05-27" "2005-05-28" "2005-05-29"
        # [8] "2005-05-30" "2005-05-31" "2005-06-01" "2005-06-02" "2005-06-03" "2005-06-04" "2005-06-05"
        
        • %A :当前语言环境中的完整工作日名称。 (也匹配输入的缩写名称。)
        • %d:以十进制数表示的月份中的日期 (01–31)。
        • %B :当前语言环境中的完整月份名称。 (也匹配输入的缩写名称。)
        • %Y : 有世纪的年份。

        数据

        x <- c("Monday 23rd May 2005", "Tuesday 24th May 2005", "Wednesday 25th May 2005", "Thursday 26th May 2005", "Friday 27th May 2005", "Saturday 28th May 2005","Sunday 29th May 2005", "Monday 30th May 2005", "Tuesday 31st May 2005", "Wednesday 1st June 2005", "Thursday 2nd June 2005", "Friday 3rd June 2005", "Saturday 4th June 2005", "Sunday 5th June 2005")
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-10-19
          • 1970-01-01
          • 2015-10-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-02-25
          相关资源
          最近更新 更多