Modérateur : Groupe des modérateurs
Code : Tout sélectionner
library(tidyverse)
Table <- data.frame(id=c(1,2,3,4,5),
nom=c("Paul","Matthieu","Camille","Mireille","Capucine"),
ville=c("Paris", "Marseille", "Lyon", "Lille", "Nantes"),
animal=c("Chat", "Chien", "Chat", "Chat", "Chien"),
note1=c(12,8,11,16,15),
note2=c(9,13,15,10,12),
note3=c(11,14,12,9,12),
note4=c(18,16,15,19,11))
table_note <- Table %>%
pivot_longer(starts_with("note"), names_to = "num", values_to = "note",
names_ptypes = list("num" = integer()), names_prefix = "note")
Code : Tout sélectionner
# A tibble: 20 x 6
id nom ville animal num note
<dbl> <fct> <fct> <fct> <int> <dbl>
1 1 Paul Paris Chat 1 12
2 1 Paul Paris Chat 2 9
3 1 Paul Paris Chat 3 11
4 1 Paul Paris Chat 4 18
5 2 Matthieu Marseille Chien 1 8
6 2 Matthieu Marseille Chien 2 13
7 2 Matthieu Marseille Chien 3 14
8 2 Matthieu Marseille Chien 4 16
9 3 Camille Lyon Chat 1 11
10 3 Camille Lyon Chat 2 15
11 3 Camille Lyon Chat 3 12
12 3 Camille Lyon Chat 4 15
13 4 Mireille Lille Chat 1 16
14 4 Mireille Lille Chat 2 10
15 4 Mireille Lille Chat 3 9
16 4 Mireille Lille Chat 4 19
17 5 Capucine Nantes Chien 1 15
18 5 Capucine Nantes Chien 2 12
19 5 Capucine Nantes Chien 3 12
20 5 Capucine Nantes Chien 4 11
Par contre que signifie names_ptypes = list("num" = integer()) ?
Est-ce que cela pourrait venir de mon intitulé de colonne ? car dans ma véritable table il s'agit d'un nom suivi d'un saut de ligne et d'une année.
Code : Tout sélectionner
names_prefix = "note"
Code : Tout sélectionner
names_prefix = "nom\\n"
Code : Tout sélectionner
table_note <- Table %>%
pivot_longer(starts_with("note\r\n"), names_to = "num", values_to = "note",
names_ptypes = list("num" = integer()), names_prefix = "note\r\n")
Code : Tout sélectionner
table_note <-
pivot_longer(Table, starts_with("note\r\n"), names_to = "num", values_to = "note",
names_ptypes = list("num" = integer()), names_prefix = "note\r\n")
Ne serait-ce pas plus logique d'écrire alors :On lit tout de la gauche vers la droite
Code : Tout sélectionner
x %>% F() -> y
Les deux.À écrire ou à s'exécuter ?
Code : Tout sélectionner
x <- runif( 1000000)
microbenchmark( identity( x), x %>% identity, times=1000000)
Code : Tout sélectionner
Unit: nanoseconds
expr min lq mean median uq max neval cld
identity(x) 0 0 90.51562 1 1 2621936 1e+06 a
x %>% identity 44048 47470 50534.02021 48753 49608 132129977 1e+06 b
Warning message:
In microbenchmark(identity(x), x %>% identity, times = 1e+06) :
Could not measure a positive execution time for 51102 evaluations.
Code : Tout sélectionner
y1 <- H( x)
y2 <- G( y1)
y <- F( y2)
Qu'est-ce qui justifie d'utiliser comme tu le fais %>% ?
Ne serait-ce pas plus logique d'écrire alors :
x %>% F() -> y
Code : Tout sélectionner
10 %>%
rnorm %>%
plot(10 %>% runif)
Retourner vers « Questions en cours »
Utilisateurs parcourant ce forum : Aucun utilisateur enregistré et 1 invité