Floor_date 不适用于 mutate 和 ifelse

发布于 2025-01-19 20:58:29 字数 717 浏览 1 评论 0原文

我试图编写一个概括的聚合函数,其中用户指定聚合级别,或者可以在所有研究日期汇总数据。 floor_date仅转换第一个日期。为什么?我该如何解决?

library(dplyr)
library(lubridate)

sTerm <- "year" # month, bimonth, quarter, season, halfyear and year, custom
sCustom <- "2023-2025"

dfDatasetOutput  <- data.frame(
  valDate=seq(as.Date("2023-01-01"), as.Date("2025-12-01"), by = "month"), 
  cat1=rnorm(36, 3500, 1000),
  cat2=rnorm(36, 2.5, 5)
)

dfDatasetOutput %>%
  mutate(valDate=ifelse(toupper(sTerm)=="CUSTOM", 
                          sCustom, 
                          as.character(floor_date(valDate, sTerm)))) 

# this works just fine
dfDatasetOutput %>%
  mutate(valDate=as.character(floor_date(valDate, sTerm)))

I am trying to write a generalize aggregation function where the user specifies the aggregation level or they can aggregate the data over all study dates. The floor_date only converts the first date. why? How can I fix this?

library(dplyr)
library(lubridate)

sTerm <- "year" # month, bimonth, quarter, season, halfyear and year, custom
sCustom <- "2023-2025"

dfDatasetOutput  <- data.frame(
  valDate=seq(as.Date("2023-01-01"), as.Date("2025-12-01"), by = "month"), 
  cat1=rnorm(36, 3500, 1000),
  cat2=rnorm(36, 2.5, 5)
)

dfDatasetOutput %>%
  mutate(valDate=ifelse(toupper(sTerm)=="CUSTOM", 
                          sCustom, 
                          as.character(floor_date(valDate, sTerm)))) 

# this works just fine
dfDatasetOutput %>%
  mutate(valDate=as.character(floor_date(valDate, sTerm)))

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

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

发布评论

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

评论(1

苍白女子 2025-01-26 20:58:29

该问题并非源于 floor_date,而是源于您使用 ifelse。根据其手册:

ifelse(test, yes, no)
ifelse returns a value with the same shape as test which is filled
with elements selected from either yes or no depending on whether 
the element of test is TRUE or FALSE.

您的测试是 toupper(sTerm)=="CUSTOM" 这是单个逻辑元素 TRUE 或 FALSE(或 NA)。因此 ifelse 的输出将是单个元素。如果测试结果为 false,它将从 as.character(floor_date(valDate, sTerm)) 中获取此元素。它只需要一个,因此将采用第一个。然后 mutate 将此单个值回收到列的长度。

如果您希望输出的长度与 valDate 相同,解决方法是重复测试,以便获得所需长度的向量作为测试:

dfDatasetOutput %>%
  mutate(valDate=ifelse(rep(toupper(sTerm)=="CUSTOM",nrow(dfDatasetOutput)), 
                        sCustom, 
                        as.character(floor_date(valDate, sTerm)))) 

为了避免意外使用 ifelse,请考虑使用 if_else 它对对象长度进行检查。

The problem does not stem from floor_date but from your use of ifelse. As per its manual:

ifelse(test, yes, no)
ifelse returns a value with the same shape as test which is filled
with elements selected from either yes or no depending on whether 
the element of test is TRUE or FALSE.

Your test is toupper(sTerm)=="CUSTOM" which is a single logical element TRUE or FALSE (or NA). So the output of ifelse will be a single element. If the test is false, it will take this element from as.character(floor_date(valDate, sTerm)). It only needs one, so will take the first one. Then mutate recycles this single value to the length of the column.

If you want the output to be the same length as valDate, a workaround would be to repeat your test so you get a vector of the desired length as a test:

dfDatasetOutput %>%
  mutate(valDate=ifelse(rep(toupper(sTerm)=="CUSTOM",nrow(dfDatasetOutput)), 
                        sCustom, 
                        as.character(floor_date(valDate, sTerm)))) 

To avoid such unintended use of ifelse, consider using if_else which runs checks on object lengths.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文