【问题标题】:R Financial Year Date ConversionR 财政年度日期转换
【发布时间】:2023-01-10 01:05:39
【问题描述】:

快速提问,是否可以转换以下字符字段以允许我在趋势图中使用它们。这些字段代表财务季度。

"Jul - Sep 2020", "Jul - Sep 2021", "Oct - Dec 2020", "Apr - Jun 2021", "Jan - Mar 2021", "Oct - Dec 2021"
"Jan - Mar 2022"

【问题讨论】:

  • 添加预期输出
  • 只想转换日期字段。他们看起来一样
  • 再次..添加预期输出..“日期字段”..什么日期?每个月的第一天、最后一个月、中间的所有月份、所有的日子……这个“日期字段”应该包含什么?日期字段不能包含间隔...(但间隔可以包含日期...)。更具体地说明您的期望。
  • 抱歉,但不知道如何更具体。我想将使用标准财务季度(例如 4 月 - 3 月、7 月 - 9 月)的文本字段从文本格式转换为日期格式。

标签: r


【解决方案1】:

正如 cmets 中所指出的,您需要假设将本季度的哪个日期作为参考。

请查看下面的示例是否是您需要的。

# Original date
x <- c("Jul - Sep 2020", "Jul - Sep 2021", "Oct - Dec 2020",
       "Apr - Jun 2021", "Jan - Mar 2021", "Oct - Dec 2021")

# Split all elements of the date
df <- data.frame(matrix(unlist(
  strsplit(as.character(x), split = " ")), ncol = 4, byrow = TRUE)
)

# Convert to date format (assuming you take the first day of the quarter)
df$original_date <- x
df$modified_date <- paste0("1", tolower(df$X1), df$X4)
df$final_date <- as.Date(df$modified_date, format = "%d%b%Y")

# Add column of the value I wont to plot
df$myvalue <- 1:6

# Order the table
df <- df[order(df$final_date),]
str(df)

# Plot labelling date with original format
plot(x = df$final_date, y = df$myvalue, type = "b",
     at = df$final_date, labels = df$original_date)

情节看起来像这样:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多