r橄榄酸酯将数据表列作为参数传递给月参数而不是固定整数

发布于 2025-01-28 06:09:03 字数 1985 浏览 3 评论 0原文

我有一个带有两个列的数据表:日期列和另一列具有整数值。我想将“整数”列中的值用作添加到日期列的月数。我有一种感觉可能需要诸如应用[或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

 dateTableProdKeep
quot;PROD_month"  <- dateTableProdKeep
quot;calc_new_date" %m+% months(1)

does not work when trying to pass a datatable column to the months argument

 dateTableProdKeep
quot;PROD_month"  <- dateTableProdKeep
quot;calc_new_date" %m+% months(dataTableProdKeep
quot;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 )
    
    dateTableProdKeep
quot;calc_new_date" <- as.Date( dateTableProdKeep
quot;calc_new_date" , format="%m/%d/%Y" )
    dateTableProdKeep
    
    dateTableProdKeep
quot;PROD_month" <- dateTableProdKeep
quot;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 )
    
    dateTableProdKeep
quot;calc_new_date" <- as.Date( dateTableProdKeep
quot;calc_new_date" , format="%m/%d/%Y" )
    dateTableProdKeep
    
    #dateTableProdKeep
quot;PROD_month" <- dateTableProdKeep
quot;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
quot;new" <- dateTableProdKeep
quot;calc_new_date" %m+% months(dateTableProdKeep
quot;Month")
    dateTableProdKeep

calc_new_date Month        new
1    2022-01-01     3 2022-04-01
2    2022-01-31     1 2022-02-28

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

埖埖迣鎅 2025-02-04 06:09:03

这是您所得到的:

tibble(x = 1:100,date = seq(as.Date("2000-01-01"),by = "months", length = length(x)),z = date %m+% months(x))

Is this what you're getting at:

tibble(x = 1:100,date = seq(as.Date("2000-01-01"),by = "months", length = length(x)),z = date %m+% months(x))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文