简体   繁体   English

将月份添加到日期 ifelse

[英]add months to a date ifelse

I want to add months to a date by conditition.我想按条件向日期添加月份。 That means if my ISO column is ET I want to add 92 month.这意味着如果我的 ISO 列是 ET 我想添加 92 个月。

When I only run this code, i recieve the dates I want.当我只运行这段代码时,我会收到我想要的日期。

ymd(paste(comb_extract_all$hv007, comb_extract_all$hv006, "01", sep = "-")) %m+% months(92)

first lines of my output:我的 output 的第一行:

 [1] "2011-04-01" "2011-04-01" "2011-04-01" "2011-04-01" "2011-04-01" "2011-04-01" "2011-04-01" "2011-04-01"
 [9] "2011-04-01" "2011-04-01" "2011-04-01" "2011-04-01" "2011-04-01" "2011-04-01" "2011-04-01" "2011-04-01"
[17] "2011-04-01" "2011-04-01" "2011-04-01" "2011-04-01" "2011-04-01" "2011-04-01" "2011-04-01" "2011-04-01"
[25] "2011-04-01" "2011-04-01" "2011-04-01" "2011-04-01" "2011-04-01" "2011-04-01" "2011-04-01" "2011-04-01"

But if I use it in an ifelse statement, it will returns numbers但是如果我在ifelse语句中使用它,它会返回数字

comb_extract_all$date <- ifelse(comb_extract_all$ISO == "ET", 
                                ymd(paste(comb_extract_all$hv007, comb_extract_all$hv006, "01", sep="-")) %m+% 
                                  months(92),
                                ymd(paste(comb_extract_all$hv007, comb_extract_all$hv006, "01", sep="-")))

My dput output with the most important columns is as follows (where you can see the "wrong" date column):我的dput最重要的列如下(您可以在其中看到“错误”日期列):

dput(comb_extract_all[1:5,c(1,5,6,23,24)])
structure(list(hhid = c("        1 27", "        1 27", "        1 27", 
"        1 27", "        1 67"), hv006 = c(8, 8, 8, 8, 8), hv007 = c(2003, 
2003, 2003, 2003, 2003), ISO = c("ET", "ET", "ET", "ET", "ET"
), date = c(15065, 15065, 15065, 15065, 15065)), row.names = c("ETPR61SV.1", 
"ETPR61SV.2", "ETPR61SV.3", "ETPR61SV.4", "ETPR61SV.5"), class = "data.frame")

One option to avoid the implicit conversion to numeric s would be to switch to dplyr::if_else :避免隐式转换为numeric的一种选择是切换到dplyr::if_else

library(lubridate)
library(dplyr)

comb_extract_all$date <- ymd(paste(comb_extract_all$hv007, comb_extract_all$hv006, "01", sep = "-"))

comb_extract_all$date <- dplyr::if_else(comb_extract_all$ISO == "ET",
  comb_extract_all$date %m+% months(92),
  comb_extract_all$date
)

comb_extract_all
#>                    hhid hv006 hv007 ISO       date
#> ETPR61SV.1         1 27     8  2003  ET 2011-04-01
#> ETPR61SV.2         1 27     8  2003  ET 2011-04-01
#> ETPR61SV.3         1 27     8  2003  ET 2011-04-01
#> ETPR61SV.4         1 27     8  2003  ET 2011-04-01
#> ETPR61SV.5         1 67     8  2003  ET 2011-04-01

Try尝试

library(tidyverse)
comb_extract_all$date <- 
  if_else(comb_extract_all$ISO == "ET", 
        (ymd(paste(comb_extract_all$hv007,
        comb_extract_all$hv006,"01", 
        sep = "-")) %m+% months(92)),                                    
  (ymd(paste(comb_extract_all$hv007,
            comb_extract_all$hv006,"01", 
            sep = "-"))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM