具有月(整数)和年(整数)的矩阵,我希望使用R以Date或POSIX格式提取它

问题描述 投票:0回答:3

我有一个功能创建到月份,所以如果在一个月的15日之前创建发票,将考虑上个月。否则它将考虑当前月份。输出存储在矩阵中(2列和4500行)。一列是整数月份,另一列是整数年份。程序和输出如下。我希望月份和年份是日期格式而不是整数,以便我可以在可视化中滑动和切块数据。非常感谢您的帮助。

# If the date is before 15th of a month, will consider previous month. Else current month
myDateFun <- function(x){
      x <- as.Date(x, format='%d-%m-%Y')
      if (day(x) < 15){
        dd <- x-14 
      }
      else {dd <- x}
      return(c(month(dd), year(dd)))
    }

    # sapply method used to absorb the function and create matrix of month and year
mat = t(sapply(CI3$invoice_date, FUN=myDateFun, simplify='matrix'))

# Output [,1] is month. [,2] is year   
mat
            [,1] [,2]
       [1,]    3 2016
       [2,]    4 2016
       [3,]    5 2016
       [4,]    6 2016
r date posixct
3个回答
0
投票

如果稍微调整功能,则不需要使用sapply。

myDateFun <- function(x){
  x <- as.Date(x, format='%d-%m-%Y')
  ifelse(lubridate::day(x) < 15, dd <- x-14, dd <- x)
  out <- format(dd, "%Y-%m")
  return(out)
}

# add year month to CI3
# year_month will be a character vector due to format function.
CI3$year_month <- myDateFun(CI3$invoice_date)

edit based on comment:

我编辑了函数,因此它可以采用额外的参数来指定年份或月份。默认是年份。非常简单的错误处理,以确保它是这些值之一。

myDateFun <- function(x, period = "year"){
  # error handling
  if(!(period %in% c("year", "month"))) stop("period should be year or month")

  x <- as.Date(x, format='%d-%m-%Y')
  ifelse(lubridate::day(x) < 15, dd <- x-14, dd <- x)
  if(period == "year"){
  out <- format(dd, "%Y")
  } else {
    out <- format(dd, "%b")
  }
  return(out)
}


CI3$year <- myDateFun(CI3$invoice_date, "year")
CI3$month <- myDateFun(CI3$invoice_date, "month")

0
投票

这是使用lubridate和purrr包的解决方案。我经常只需要一个日期的月份和年份,所以我只把这一天默认为1并忽略它。

以下是您格式的一些示例数据:

    library(tidyverse)
    library(lubridate)

     x <- data_frame(date = c("03/01/2018", "01/02/2015", "03/04/2006", "25/12/2006", "15/01/2014"))

这是使用lubridate的函数:

    AltDateFun <- function(x) {
        x <- dmy(x)
        if (day(x) < 15) {
            x <- x - months(1)
            day(x) <- 1
            return(x)
        }
        else {
            day(x) <-1
            return(x)
        }
    }

假设您的发票日期是数据框中的字符列,其日期格式为dmy:

    z <- map_df(x, AltDateFun)

    # A tibble: 5 x 1
    x         
    <date>    
    1 2017-12-01
    2 2015-01-01
    3 2006-03-01
    4 2006-11-01
    5 2013-12-01

编辑:

要在单独的列中获取月份和年份,请执行以下操作:

    z %>% mutate(m = month(x), y = year(x))

    # A tibble: 5 x 3
    x              m     y
    <date>     <dbl> <dbl>
    1 2017-12-01 12.0   2017
    2 2015-01-01 1.00   2015
    3 2006-03-01 3.00   2006
    4 2006-11-01 11.0   2006
    5 2013-12-01 12.0   2013

0
投票

工作。谢谢大家的回答。只是分享我使用的代码。更新了以下代码

myDateFun <- function(x, period = "year") {
  # error handling
  if (!(period %in% c("year", "month")))
    stop("period should be year or month")

  x <- as.Date(x, format = '%d-%m-%Y')
  ifelse(lubridate::day(x) < 15, dd <- x - 14, dd <- x)
  if (period == "year") {
    out <- format(dd, "%Y")
  } else {
    out <- format(dd, "%b")
  }
  return(out)
}

CI3$invyr <- myDateFun(CI3$invoice_date, "year")
CI3$invmon <- myDateFun(CI3$invoice_date, "month")
CI3$date_m_Y = paste(CI3$invmon, CI3$invyr, sep = "-")
© www.soinside.com 2019 - 2024. All rights reserved.