Home > Mobile >  How to make conditions for this case in a R Code
How to make conditions for this case in a R Code

Time:12-22

I would like to make a brief condition in the code below. Note that I have as input data dmda<-"2021-07-01", CategoryChosse<-"FDE", DTest<-"0". If you run the code up to med, you will notice that in med there is no line with DTest="0". As you can see in Dx I'm using dates before date1 (28/06) to do the analyses, notice that I don't have any DTT = "0" in days before date1, so in med I don't have anything.

Code to obtainmed

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<-"0"

 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"))
  
  med<-PV %>%
        group_by(Category,Week,DTT) %>%
        summarize(across(ends_with("PV"), median))
> med
# A tibble: 2 x 8
# Groups:   Category, Week [2]
  Category Week      DTT   DR01_PV DR02_PV DR03_PV DR04_PV DR05_PV
  <chr>    <chr>     <chr>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
1 ABC      Wednesday Hol         0       0      -5      -1      -1
2 FDE      Thursday  Hol         0      -1      -4      -3      -3

From there, I would like to make two conditions to generate a variable called SPV

If I have Dtype="0" in med do:

  SPV<-df1%>%
    inner_join(med, by = c('Category', 'Week','DTT')) %>%
    mutate(across(matches("^DR0\\d $"), ~.x   
                    get(paste0(cur_column(), '_PV')),
                  .names = '{col}_{col}_PV')) %>%
    select(date1:Category, DR01_DR01_PV:last_col())

SPV <- SPV %>%
    filter(date2 == dmda, Category == CategoryChosse, DTT==DTest)    

If I don't have Dtype="0" in med do:

     SPV<-df1%>%
        inner_join(med, by = c('Category', 'Week')) %>%
        mutate(across(matches("^DR0\\d $"), ~.x   
                        get(paste0(cur_column(), '_PV')),
                      .names = '{col}_{col}_PV')) %>%
        select(date1:Category, DR01_DR01_PV:last_col())
  
    SPV <- SPV %>%
        filter(date2 == dmda, Category == CategoryChosse)

CodePudding user response:

Based on the condition, an if/else expression can be used

if(any(med$DTT == DTest & med$Category== CategoryChosse, na.rm = TRUE)) {

  SPV<-df1%>%
    inner_join(med, by = c('Category', 'Week','DTT')) %>%
    mutate(across(matches("^DR0\\d $"), ~.x   
                    get(paste0(cur_column(), '_PV')),
                  .names = '{col}_{col}_PV')) %>%
    select(date1:Category, DR01_DR01_PV:last_col())

    SPV <- SPV %>%
       filter(date2 == dmda, Category == CategoryChosse, DTT==DTest)  
    } else {
    
      SPV <- df1%>%
           inner_join(med, by = c('Category', 'Week')) %>%
           mutate(across(matches("^DR0\\d $"), ~.x   
                           get(paste0(cur_column(), '_PV')),
                         .names = '{col}_{col}_PV')) %>%
           select(date1:Category, DR01_DR01_PV:last_col())
  
       SPV <- SPV %>%
           filter(date2 == dmda, Category == CategoryChosse)
           }
  •  Tags:  
  • r
  • Related