3

I'm having problems with the pivot_longer function in datas. Could you help me solve it?

In this question works normally: How to adjust error when I have 0 values for graph generation. However, in this previous question I am not using the DTT column, in this current question yes.

library(dplyr)

df1 <- structure(
  list(date1= c("2021-06-28","2021-06-28","2021-06-28"),
       date2 = c("2021-06-30","2021-06-30","2021-07-02"),
       DTT= c(NA,NA,"Hol"),
       Week= c("Wednesday","Wednesday","Friday"),
       Category = c("ABC","FDE","ABC"),
       DR1 = c(4,1,0),
       DR01 = c(4,1,0), DR02= c(4,2,0),DR03= c(9,5,0),
       DR04 = c(5,4,0),DR05 = c(5,4,0)),
  class = "data.frame", row.names = c(NA, -3L))


dmda<-"2021-07-02"
CategoryChosse<-"ABC"
DTest<-"Hol"

  x<-df1 %>% select(starts_with("DR0"))
  
  x<-cbind(df1, setNames(df1$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))
  
  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<-data.frame(SPV)
  
  mat1 <- df1 %>%
    filter(date2 == dmda, Category == CategoryChosse, DTT==DTest) %>%
    select(starts_with("DR0")) %>%
    pivot_longer(cols = everything()) %>%
    arrange(desc(row_number())) %>%
    mutate(cs = cumsum(value)) %>%
    filter(cs == 0) %>%
    pull(name)
  
  (dropnames <- paste0(mat1,"_",mat1, "_PV"))
  
  SPV <- SPV %>%
    filter(date2 == dmda, Category == CategoryChosse, DTT==DTest) %>%
    select(-any_of(dropnames))
  
  if(length(grep("DR0", names(SPV))) == 0) {
    SPV[mat1] <- NA_real_
  }
  
  datas <-SPV %>%
    filter(date2 == ymd(dmda)) %>%
    group_by(Category, DTT) %>%
    summarize(across(starts_with("DR0"), sum)) %>%
    pivot_longer(cols= -Category, names_pattern = "DR0(.+)", values_to = "val") %>%
    mutate(name = readr::parse_number(name))
  colnames(datas)[-1]<-c("Days","Numbers")

Error: Can't combine `DTT` <character> and `DR05` <double>.
Run `rlang::last_error()` to see where the error occurred.
Anoushiravan R
  • 21,622
  • 3
  • 18
  • 41
  • I think in the `pivot_longer`, you need to only include the 'DRO' columns because `-Category` implies all other columns and there is `DTT` which is character i.e. ` `pivot_longer(cols= starts_with("DR0"), names_pattern = "DR0(.+)",` – akrun Dec 20 '21 at 19:28
  • That's it, thanks a lot akrun! Can you leave it in the form of an answer so that I can accept it? Besides, I have two questions: how will `colnames` look like after its change? and like `DTest` if my `DTT` is `NA`. Is `DTest = ""` or `DTest = NA` or `DTest = "NA"`? –  Dec 20 '21 at 19:46
  • I posted a soluiton. Please check – akrun Dec 20 '21 at 19:54
  • Yes, that's it,. And in relation of my second question about DTest? How would it look if `DTT` were `NA`? For example, for 30/06, Category ABC –  Dec 20 '21 at 19:59
  • let me test it for that – akrun Dec 20 '21 at 20:01
  • When the value for `DTest` is `""` or `NA`, you don't get any error. It is giving `> datas # A tibble: 0 × 4 # Groups: Category [0] # … with 4 variables: Category , DTT , Days , Numbers ` – akrun Dec 20 '21 at 20:04
  • I asked a question more or less about this https://stackoverflow.com/questions/70428206/problems-generating-variable-when-i-have-na-data-in-r-code I think it's better to understand. –  Dec 20 '21 at 21:38

1 Answers1

3

pivot_longer checks the column types and by specifying -Category in cols, it will select all the remaining columns. But, in the OP's dataset, there is a character column 'DTT' in addition to other numeric columns ('DR0'). An option is to either remove the 'DTT' (by %>% select(-DTT) %>% pivot_longer(..) and use the OP's code or use cols = starts_with("DR0")

library(dplyr)
library(tidyr)
datas <- SPV %>%
    filter(date2 == ymd(dmda)) %>%
    group_by(Category, DTT) %>%
    summarize(across(starts_with("DR0"), sum), .groups = "drop") %>%
    pivot_longer(cols= starts_with("DR0"), names_pattern = "DR0(.+)", 
         values_to = "val") %>%
    mutate(name = readr::parse_number(name))

-output

> head(datas)
# A tibble: 5 × 4
  Category DTT    name   val
  <chr>    <chr> <dbl> <dbl>
1 ABC      Hol       5    NA
2 ABC      Hol       4    NA
3 ABC      Hol       3    NA
4 ABC      Hol       2    NA
5 ABC      Hol       1    NA

Regarding the change of column names, here there are 4 columns. So, we may need

colnames(datas)[-c(1, 2)] <- c("Days","Numbers")
akrun
  • 874,273
  • 37
  • 540
  • 662