下面的代碼作業正常,但我想創建另一個名為JOVafter 的變數SPV。此變數將具有如下條件:
如果我有"Category", "Week"and "DTT"in group_cols, 請執行以下操作:
SPV %>% filter(date2 == dmda, Category == CategoryChosse, DTT==DTest)
如果我有 "Category"和"Week"in group_cols,請執行以下操作:
SPV %>% filter(date2 == dmda, Category == CategoryChosse)
如果我只有"Category"in group_cols,請執行以下操作:
SPV %>% filter(date2 == dmda)
可執行代碼如下
library(dplyr)
library(tidyverse)
library(lubridate)
df1 <- structure(
list(date1= c("2021-06-28","2021-06-28","2021-06-28","2021-06-28"),
date2 = c("2021-06-23","2021-06-24","2021-06-30","2021-07-01"),
DTT= c("Hol","Hol","Hol",0),
Week= c("Wednesday","Thursday","Wednesday","Thursday"),
Category = c("ABC","FDE","ABC","FDE"),
DR1 = c(4,1,1,2),
DR01 = c(4,1,2,3), DR02= c(4,2,0,2),DR03= c(9,5,0,1),
DR04 = c(5,4,3,2),DR05 = c(5,4,0,2)),
class = "data.frame", row.names = c(NA, -4L))
dmda<-"2021-07-01"
CategoryChosse<-"FDE"
DTest<-"Hol"
Wk<-"Thursday"
Dx<-subset(df1,df1$date2<df1$date1)
x<-Dx %>% select(starts_with("DR0"))
x<-cbind(Dx, setNames(Dx$DR1 - x, paste0(names(x), "_PV")))
PV<-select(x, date2,Week, Category, DTT, DR1, ends_with("PV"))
group_cols <-
if (any(PV$DTT == DTest & PV$Week == Wk, na.rm = TRUE)) {
c("Category", "Week", "DTT")
} else if (any(PV$Week == Wk & PV$Category == CategoryChosse & PV$DTT != DTest, na.rm=TRUE)) {
c("Category", "Week")
} else {
"Week"
}
med <- PV %>%
group_by(across(all_of(group_cols))) %>%
summarize(across(ends_with("PV"), median),.groups = 'drop')
SPV <- df1 %>%
inner_join(med, by = group_cols) %>%
mutate(across(matches("^DR0\\d $"), ~.x
get(paste0(cur_column(), '_PV')),
.names = '{col}_{col}_PV')) %>%
select(date1:Category, DR01_DR01_PV:last_col())
uj5u.com熱心網友回復:
嘗試:
SPV %>%
filter(
date2 == dmda,
!("Category" %in% group_cols) | Category == CategoryChosse,
!all(c("Category", "DTT") %in% group_cols) | DTT == DTest
)
這是您的條件的字面翻譯。但是,如果我沒看錯的話,可以將其簡化為
SPV %>%
filter(
date2 == dmda,
!("Category" %in% group_cols) | Category == CategoryChosse,
!("DTT" %in% group_cols) | DTT == DTest
)
如果您曾經想象允許"DTT"而不是"Category"在您的group_cols. (即使這永遠不會發生,這也有效。)
uj5u.com熱心網友回復:
您可以將不同的案例存盤在一個串列中,并根據需要提取元素。
- 更改此函式以回傳向量和串列索引。
filter_condition <-
if (any(PV$DTT == DTest & PV$Week == Wk, na.rm = TRUE)) {
1:3 # if you had options out of order, you could have something like c(1, 3)
} else if (any(PV$Week == Wk & PV$Category == CategoryChosse & PV$DTT != DTest, na.rm=TRUE)) {
1:2
} else {
1
}
- 創建用于分組和過濾的向量和串列。
group_cols <- c("Week", "Category", "DTT")
filter_opts <- rlang::exprs(date2 == dmda,
Category == CategoryChosse,
DTT == DTest)
- 改變你的
group_by()基于你在這種情況下,從變數來拉。
med <- PV %>%
group_by(across(all_of(group_cols[filter_condition]))) %>%
summarize(across(ends_with("PV"), median),.groups = 'drop')
- 和以前一樣的代碼。
inner_join(med, by = group_cols) %>%
mutate(across(matches("^DR0\\d $"), ~.x
get(paste0(cur_column(), '_PV')),
.names = '{col}_{col}_PV')) %>%
select(date1:Category, DR01_DR01_PV:last_col())
- 根據您所處的情況應用過濾條件。
SPV %>%
filter(!!!filter_opts[filter_condition])
正如另一個答案中提到的,您可以縮短它,因為它似乎date2總是需要的。但這為此類事情制定了一些框架。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/391173.html
標籤:r
下一篇:變換單位矩陣
