使用lubridate根据日期创建因子

问题描述 投票:1回答:1

我试图找到合适的答案,但所有案例都比我的案例简单得多。我需要根据我拥有的数据框中的日期信息创建一个4级(nov,end_feb,end_apr等)因子,然后将其添加为列。而且,由于我拥有的实际df超过80万行,因此我需要代码才能快速运行]

[到目前为止是lubridate%within%的内容。它确实可以工作,但是由于效率低下而导致速度非常慢,因为我不得不诉诸于用sapply(df, sub_period_gen(date))创建一个新列。理想情况下,我需要一种方法来确保解决方案是矢量化的,因为我还有其他一些在相同数据帧上运行的因子生成器,而且生成速度也很慢。

sub_period_gen <- function(x){
  i_1 <- ymd("2019-11-01")%--% ymd("2019-11-30")
  i_2 <- ymd("2020-02-24")%--% ymd("2020-02-29")
  i_3 <- ymd("2020-04-24")%--% ymd("2020-04-30")
  if (x %within% i_1){
    return("nov")  # return case one
  } else if (x %within% i_2){
    return("end_feb")  # return case two
  } else if (x %within% i_3){
    return("end_apr")  # return case three
  } else{
    return("other")  # return case four
  }
}

提前感谢!

EDIT:

我对解决方案进行了一些优化,但看起来仍然不是最佳而且很难修改。另外,我将间隔移到了全局环境中
sub_period_gen <- function(x){
  return(ifelse(x %within% i_1,"nov",ifelse(x %within% i_2,"end_feb",ifelse(x %within% i_3,"end_apr","other"))))
  }

我的问题与this one不同,因为我的约会日期确实没有规律,休息时间是针对特定分析的。

EDIT 2:

示例输入:
library(lubridate)
toy <- tibble(date = ymd("2019-11-12","2020-03-11","2020-01-31","2019-12-19","2019-12-04","2020-01-21","2020-01-31","2020-02-16",
              "2020-02-28","2020-03-20","2020-02-08","2020-03-23","2020-01-22","2020-02-18","2020-03-19","2019-11-22",
              "2020-01-14","2020-03-04","2019-12-02","2019-11-03","2020-02-27","2020-02-13","2019-11-17","2020-03-17",
              "2020-04-14","2019-12-19","2019-11-05","2020-01-11","2020-04-25","2019-11-24"))

期望的输出:

>  date         sub_period
>   <date>     <chr>     
> 1 2019-11-12 nov       
> 2 2020-03-11 other
> 3 2020-01-31 other   
> 4 2019-12-19 other   
> 5 2019-12-04 other   
> 6 2020-01-21 other   
> 7 2020-02-29 end_feb   
> 8 2020-02-16 other   
> 9 2020-04-28 end_apr 

我试图找到合适的答案,但所有案例都比我的案例简单得多。我需要根据数据中的日期信息创建一个4级(nov,end_feb,end_apr等)因子...

r datetime dplyr lubridate
1个回答
1
投票

这是case_when中的dplyr的一种方法:

© www.soinside.com 2019 - 2024. All rights reserved.