有月(整数)和年(整数)的矩阵,我希望使用R以日期或POSIX格式提取它
我创建了一个函数来到达月份,因此如果在第十五个月之前创建一个发票,将考虑上个月。否则将考虑当前月份。输出存储在一个矩阵中(2列4500行)。一列用整数表示月份,另一列用整数表示年份。程序和输出如下。我希望月份和年份是日期格式,而不是整数,这样我就可以在可视化中滑动和分割数据。谢谢你的帮助有月(整数)和年(整数)的矩阵,我希望使用R以日期或POSIX格式提取它,r,date,posixct,R,Date,Posixct,我创建了一个函数来到达月份,因此如果在第十五个月之前创建一个发票,将考虑上个月。否则将考虑当前月份。输出存储在一个矩阵中(2列4500行)。一列用整数表示月份,另一列用整数表示年份。程序和输出如下。我希望月份和年份是日期格式,而不是整数,这样我就可以在可视化中滑动和分割数据。谢谢你的帮助 # If the date is before 15th of a month, will consider previous month. Else current month myDateFun <-
# 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
<>代码>如果日期在第十五个月之前,将考虑上个月。当月其他
myDateFun如果你稍微调整一下你的功能,你就不需要使用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)
myDateFun这里有一个使用lubridate和purr包的解决方案。我通常只需要一个日期的月份和年份,所以我只是将日期默认为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"))
库(tidyverse)
图书馆(lubridate)
x有效。谢谢大家的回答。只是分享我用过的代码。更新代码如下
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 = "-")
myDateFun谢谢,它起作用了。我希望月份和年份以%m和%Y格式作为单独的列。如何获取itI在答案中添加了一个新函数。您可以选择报告所需的日期范围。我希望输入%b(或%m)和%Y,以便在可视化中选择日期范围。那么您希望的确切输出是什么?2016-02、2016和02或2016和“二月”?还是2016年2月?还是2016年和02年以及“2月”?所有这些都可以在函数的格式函数和if语句中进行调整。我希望“Feb”存储在一列中,“2016”存储在一列中。那么要创建的两个新变量>z是字符格式的发票日期吗?该错误告诉我lubridate找不到dmy格式的日期字符串。我编辑了上面的代码,以显示我使用过的库以及如何将x放入数据框中。>z Q(CI3$invoice_date是日期格式)。CI3$invoice_date是什么格式?错误消息告诉您问题所在:您需要为函数提供正确的字符格式。它很好用。
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)
}
}
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
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 = "-")