Home > Mobile >  Problems with pivot_longer function in R code
Problems with pivot_longer function in R code

Time:12-21

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.

CodePudding user response:

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")
  • Related