update package tidyr with nest and unnest

Hi everyone,
I have this (reproducible) code but with this update, nest and unnest don't work. I try to convert nest and unnest with nest_legacy and unnest_legacy as proposed... But I have these errors : Error: unexpected ',' in:
" group_by(SegmentRubisLibelle) %>%
nest(data = ('prix.moyen',"
Error in map(pmf_nested, fonctiondeciles) : object 'pmf_nested' not found

library(tidyverse)
library(purrr)
library(dplyr)
fonctiondeciles <- function(pmf, nb_classes = 10){
  
  #Si les prix sont donnés sous la forme d'un tibble, extrait la premiere colonne
  if(is.tibble(pmf)){pmf <- pmf %>% pull(1)}
  
  #On calcule les bornes 
  bornes <- quantile(x=pmf,probs=seq(from=0,to=1,length.out=nb_classes + 1 ),
                     names=  FALSE)
  breaks <- unique(bornes) #On prend l'unique car parfois il y a des doublons 
  cpf <- cut(x=pmf,breaks=unique(bornes), labels = c(1:(length(breaks) - 1)), 
             include.lowest = TRUE)
  
  return(cpf)
}
partie2<-structure(list(ArticleCode = c("00028691287", "00003001881", 
"00030130007", "00001062565", "00003000675", "00001058954", "00117408479", 
"00001062525", "01887037948", "00003001829"), ArticleLibellecourt = c("Airpuls4 2 allures 240", 
"VIGI MH 4P 200-440V CA", "BAP LEDS 2000 LM - IP 55", "BAES AMB LEDS 400LMáCONNECTE", 
"NSX250F 4P SANS DECLENCHE", "CE 4NFC + AU + 32A 4P+T 400V", 
"GUELL 3/A40/W 351 40K-94 1-10V", "BAES EVAC IP43 SATI CONNECTE", 
"C120N 4P 125A COURBE C", "DECLENCHEUR TM250D 4P4D"), SegmentRubisLibelle = c("Génie climatique", 
"Distribution d'énergie", "Communication et sécurité", "Communication et sécurité", 
"Distribution d'énergie", "Distribution d'énergie", "Eclairage", 
"Communication et sécurité", "Distribution d'énergie", "Distribution d'énergie"
), PyrFournisseurNiv5Libelle = c("AIRPULS4 2 ALLURES", "Distribution électrique", 
"BAES", "Sécurité tertiaire", "Distribution électrique", "Divers industrie", 
"OUTDOOR", "Sécurité tertiaire", "Distribution électrique", "Distribution électrique"
), occurence = c(4, 4, 8, 82, 4, 4, 4, 232, 6, 4), CATOTAL = c(7212, 
4144, 12058, 54014, 2500, 2448, 5764, 90922, 1964, 1914), Prix.Achat = c(828.432, 
1007.36, 485.622, 183.483, 607.945, 206.336, 520, 58.197, 363.01, 
465.555), Prix.Tarif = c(1673.6, 2035.07, 1173, 423.5, 1228.17, 
828.1, 1048, 148.8, 748.47, 940.52), politique.planification = c(NA, 
"0B", "0B", "0B", "0B", "0C", "0E", "0A", "0B", "0B"), qtité = c(NA, 
9, 41, 59, 31, 0, 23, 631, 12, 10), Prix.mini = c(943.5, 814.03, 
339.3, 137.62, 491.27, 229.63, 786, 39, 203.96, 376.21), `0.25` = c(943.5, 
814.03, 419.13, 146.02, 491.27, 229.63, 786, 43.4875, 227.8175, 
376.21), prix.moyen = c(1802.695, 1035.83, 557.44, 198.249512195122, 
625.125, 363.245, 917, 53.8106896551724, 327.456666666667, 478.715
), `0.75` = c(2661.89, 1257.63, 722.36, 227.97, 758.98, 496.86, 
1048, 57.125, 434.1125, 581.22), prix.max = c(2661.89, 1257.63, 
722.36, 289.98, 758.98, 496.86, 1048, 91.24, 479.02, 581.22), 
    Remise.min = c(0.563754780114723, 0.400000982767178, 0.28925831202046, 
    0.32495867768595, 0.40000162843906, 0.277297427846878, 0.75, 
    0.262096774193548, 0.27250257191337, 0.400002126483222), 
    Remise.moyenne = c(1.07713611376673, 0.508989862756564, 0.475225916453538, 
    0.468121634463098, 0.508988983609761, 0.438648713923439, 
    0.875, 0.361630978865406, 0.437501391728014, 0.508989707821205
    ), Remise.max = c(1.59051744741874, 0.61797874274595, 0.615822676896846, 
    0.684722550177096, 0.617976338780462, 0.6, 1, 0.613172043010753, 
    0.639998931152885, 0.617977289159189), Valeur.unitaire = c(828.432, 
    1007.36, 485.622, 183.483, 607.945, 206.336, 520, 58.197, 
    363.01, 465.555), IQR = c(1718.39, 443.6, 303.23, 81.95, 
    267.71, 267.23, 262, 13.6375, 206.295, 205.01)), row.names = c(NA, 
10L), class = "data.frame")
nest<-nest_legacy
unnest<-unnest_legacy
partie2 %>%
  group_by(SegmentRubisLibelle) %>%
  nest(data = ('prix.moyen',ArticleCode), .key = "pmf_nested") %>% 
  mutate(echelleprix = map(pmf_nested,  fonctiondeciles)) %>% 
  unnest(pmf_nested, echelleprix)

Can you help me please? I hope to be clear :sweat_smile:

The first problem can be solved by changing these lines:

partie2 %>%
  group_by(SegmentRubisLibelle) %>%
  nest(prix.moyen, ArticleCode, .key = "pmf_nested")

The next error occurs within the fonctiondeciles() function, where the error message hints that the lengths 'breaks' and 'labels' differ. Hopefully you can debug that one yourself.

Incidentally, overwriting function names like this doesn't look good practice to me, but I don't think it's doing any actual harm:

nest<-nest_legacy
unnest<-unnest_legacy

Thanks for your help. But this functiondeciles() have not problem because it runs with the old version of tidyr, so this functiondeciles() is just I think.
So, what I do?

If you remove the labels part of the cut function then the code appears to run ok:

fonctiondeciles <- function(pmf, nb_classes = 10){
  
  #Si les prix sont donnés sous la forme d'un tibble, extrait la premiere colonne
  if(is.tibble(pmf)){pmf <- pmf %>% pull(1)}
  
  #On calcule les bornes 
  bornes <- quantile(x=pmf,probs=seq(from=0,to=1,length.out=nb_classes + 1 ),
                     names=  FALSE)
  breaks <- unique(bornes) #On prend l'unique car parfois il y a des doublons 
  cpf <- cut(x=pmf,breaks=unique(bornes), 
             include.lowest = TRUE)
  
  return(cpf)
}

I can't really see how the function could have worked with the labels = c(1:(length(breaks) - 1)) part included, but t's possible I don't understand what you were trying to achieve in that part.

Hi,
thanks but I try to take off this but it's not good, I can't!
I think really that the problem is the new functions nest and unnest.
I want to have with this function a scale at 1 to 10 for each prix.moyen of each ArticleCode.
Also, I have determined the number of class (10) and the dataset.
It's clear?

I want this for echelleprix :
echelleprix

I don't understand your desired output. Your example dataset only has ten rows.

If you could produce the desired output for those ten rows the solution might become clearer.

It's this output I want!! But this does not work anymore with this update. It's here the problem...

This seems odd:

  • You use nest in a pipe, so I believe you want first argument data to be the table from previous line. However, you set data to something here
  • ('prix.moyen',ArticleCode) : do you mean c('prix.moyen',ArticleCode) ? Why parenthesis only ?

The error you got make me wonder about this line :thinking:

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.