R : dplyr : Comprendre les étapes de %>%
Bonjour,
J'ai récupéré, je ne sais plus où, ce programme :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| library(gapminder)
new_gap <- gapminder
new_gap %>% head()
new_gap %>%
select(country, year, continent, lifeExp) %>%
group_by(continent, country) %>%
## within country, take (lifeExp in year i) - (lifeExp in year i - 1)
## positive means lifeExp went up, negative means it went down
mutate(le_delta = lifeExp - lag(lifeExp)) %>%
## within country, retain the worst lifeExp change = smallest or most negative
summarize(worst_le_delta = min(le_delta, na.rm = TRUE)) %>%
## within continent, retain the row with the lowest worst_le_delta
top_n(-1, wt = worst_le_delta) %>% ## top_n() : voir plus loin
arrange(worst_le_delta) |
et pour mieux le comprendre, j'ai voulu le décortiquer par étape... et je n'y arrive pas (j'ai déjà fait plusieurs essais).
Pouvez-vous m'aider ? Merci
Par étape :
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| library(gapminder)
head(gapminder)
names(gapminder)
glimpse(gapminder)
select(gapminder, country, year, continent, lifeExp)
group_by(gapminder, continent, country)
gapminder_ <-mutate(gapminder, le_delta = lifeExp - lag(lifeExp))
gapminder__<-summarize(gapminder_, worst_le_delta = min(le_delta, na.rm = TRUE))
gapminder__
str(gapminder__)
top_n(gapminder__, -1, wt = worst_le_delta)
arrange(worst_le_delta) |
Merci.