[英]add_months function in Spark R
I have a variable of the form "2020-09-01".我有一个“2020-09-01”形式的变量。 I need to increase and decrease this by 3 months and 5 months and store it in other variables.
我需要将其增加和减少 3 个月和 5 个月,并将其存储在其他变量中。 I need a syntax in Spark R.Thanks.
我需要 Spark R 中的语法。谢谢。 Any other method will also work.Thanks, Again In R following code works fine
任何其他方法也可以使用。谢谢,再次在 R 中,以下代码可以正常工作
y <- as.Date(load_date,"%Y-%m-%d") %m+% months(i)
The code below didn't work.下面的代码不起作用。 Error says
错误说
unable to find an inherited method for function 'add_months' for signature '"Date", "numeric"无法为签名“日期”、“数字”找到函数“add_months”的继承方法
loaddate = 202009
year <- substr(loaddate,1,4)
month <- substr(loaddate,5,6)
load_date <- paste(year,month,"01",sep = "-")
y <- as.Date(load_date,"%Y%m%d")
y1 <- add_months(y,-3)
Expected Result - 2020-06-01预期结果 - 2020-06-01
The lubridate
package makes dealing with dates much easier. lubridate
包使处理日期变得更加容易。 Here I have shuffled as.Date
up a step, then simply subtract 3 months
.在这里,我将
as.Date
改组了一步,然后简单地减去 3 months
。
library(lubridate)
loaddate = 202009
year <- substr(loaddate,1,4)
month <- substr(loaddate,5,6)
load_date <- as.Date(paste(year,month,"01",sep = "-"))
new_date <- load_date - months(3)
new_date
Output: new_date
输出:
Date[1:1], format: "2020-06-01"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.