r橄榄酸酯将数据表列作为参数传递给月参数而不是固定整数
我有一个带有两个列的数据表:日期列和另一列具有整数值。我想将“整数”列中的值用作添加到日期列的月数。我有一种感觉可能需要诸如应用[或lapply]之类的东西以遍历行,而是因为传递数据表列不是从技术上讲是标量输入值,而是多个标量的向量。关于如何实现这一目标有什么快速的想法?
当我尝试将数据列列传递到几个月参数时,我的数据集中的工作
dateTableProdKeep$"PROD_month" <- dateTableProdKeep$"calc_new_date" %m+% months(1)
不起作用,
dateTableProdKeep$"PROD_month" <- dateTableProdKeep$"calc_new_date" %m+% months(dataTableProdKeep$"Month")
这是一个示例,其中1个作为参数到几个月的工作。我希望这个价值来自“月”专栏。
library(lubridate)
#library(tibble)
calc_new_date <- c( "1/1/2022" , "1/31/2022" )
Month <- c( 3 , 1 )
dateTableProdKeep <- data.frame( calc_new_date , Month )
dateTableProdKeep$"calc_new_date" <- as.Date( dateTableProdKeep$"calc_new_date" , format="%m/%d/%Y" )
dateTableProdKeep
dateTableProdKeep$"PROD_month" <- dateTableProdKeep$"calc_new_date" %m+% months(1)
dateTableProdKeep
dateTableProdKeep
calc_new_date Month PROD_month
1 1/1/2022 3 2022-01-01
2 1/31/2022 1 2022-01-31
使用解决方案示例的更新代码改编自@martingal,并应用于我的原始问题:
library(lubridate)
#library(tibble)
calc_new_date <- c( "1/1/2022" , "1/31/2022" )
Month <- c( 3 , 1 )
dateTableProdKeep <- data.frame( calc_new_date , Month )
dateTableProdKeep$"calc_new_date" <- as.Date( dateTableProdKeep$"calc_new_date" , format="%m/%d/%Y" )
dateTableProdKeep
#dateTableProdKeep$"PROD_month" <- dateTableProdKeep$"calc_new_date" %m+% months(1)
dateTableProdKeep
#tibble(x = 1:100,date = seq(as.Date("2001-01-31"),by = "months", length = length(x)),z = date %m+% months(x))
dateTableProdKeep$"new" <- dateTableProdKeep$"calc_new_date" %m+% months(dateTableProdKeep$"Month")
dateTableProdKeep
calc_new_date Month new
1 2022-01-01 3 2022-04-01
2 2022-01-31 1 2022-02-28
I have a data table with two columns: a date column and another column with an integer value. I'd like to use the value in the integer column as the number of months to add to the date column. I have a feeling this may require something like apply [or lapply] to traverse the rows instead since passing a datatable column is not technically a scalar input value but rather a vector of multiple scalars. Any quick thoughts on how to achieve this?
works in my dataset
dateTableProdKeepquot;PROD_month" <- dateTableProdKeepquot;calc_new_date" %m+% months(1)
does not work when trying to pass a datatable column to the months argument
dateTableProdKeepquot;PROD_month" <- dateTableProdKeepquot;calc_new_date" %m+% months(dataTableProdKeepquot;Month")
Here's a sample where passing 1 as argument to months works. I'd like that value to come from the Month column.
library(lubridate)
#library(tibble)
calc_new_date <- c( "1/1/2022" , "1/31/2022" )
Month <- c( 3 , 1 )
dateTableProdKeep <- data.frame( calc_new_date , Month )
dateTableProdKeepquot;calc_new_date" <- as.Date( dateTableProdKeepquot;calc_new_date" , format="%m/%d/%Y" )
dateTableProdKeep
dateTableProdKeepquot;PROD_month" <- dateTableProdKeepquot;calc_new_date" %m+% months(1)
dateTableProdKeep
dateTableProdKeep
calc_new_date Month PROD_month
1 1/1/2022 3 2022-01-01
2 1/31/2022 1 2022-01-31
UPDATE code with solution example adapted from @MartinGal and applied to my original question:
library(lubridate)
#library(tibble)
calc_new_date <- c( "1/1/2022" , "1/31/2022" )
Month <- c( 3 , 1 )
dateTableProdKeep <- data.frame( calc_new_date , Month )
dateTableProdKeepquot;calc_new_date" <- as.Date( dateTableProdKeepquot;calc_new_date" , format="%m/%d/%Y" )
dateTableProdKeep
#dateTableProdKeepquot;PROD_month" <- dateTableProdKeepquot;calc_new_date" %m+% months(1)
dateTableProdKeep
#tibble(x = 1:100,date = seq(as.Date("2001-01-31"),by = "months", length = length(x)),z = date %m+% months(x))
dateTableProdKeepquot;new" <- dateTableProdKeepquot;calc_new_date" %m+% months(dateTableProdKeepquot;Month")
dateTableProdKeep
calc_new_date Month new
1 2022-01-01 3 2022-04-01
2 2022-01-31 1 2022-02-28
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是您所得到的:
Is this what you're getting at: