将系数转换为R中箱线图的日期不成功,带“不成功”;"&引用;nd"&引用;rd"&引用;";添加到日期
我拥有的数据集包含以下当前被视为因素的日期列表:将系数转换为R中箱线图的日期不成功,带“不成功”;"&引用;nd"&引用;rd"&引用;";添加到日期,r,boxplot,as.date,R,Boxplot,As.date,我拥有的数据集包含以下当前被视为因素的日期列表: Interview_Date = c("Monday 23rd May 2005", "Tuesday 24th May 2005", "Wednesday 25th May 2005", "Thursday 26th May 2005", "Friday 27th May 200
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")
但它目前不起作用,因为它是一个因素变量。我该怎么办?或者是否有其他方法创建箱线图?您可以删除序号部分(即st、nd、rd、th),然后转换为
日期对象
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"
使用润滑油润滑
:
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,...
库(tidyverse)
图书馆(lubridate)
df可以使用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
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")