R Style for long Pipes
I often write long data wrangling pipelines and wonder how to best break them up or make them more readable.
I am aware of the R tidyverse style guide on pipes but it doesnt really go into detail on my question.
So, what do yo do?
- Write one long pipe?
- Break it up into chunks with reassignment in between ?
- Write one long pipeline but separate it visually with comments in between?
Example
x <-
x %>%
mutate(
vyears =
case_when(
year == 2014 ~ vdays / 365,
year == 2013 ~ vdays / 365,
TRUE ~ NA_real_
),
vyears = as.integer(round(vyears))
) %>%
select(-vdays) %>%
relocate(vyears, .after = n_pers) %>%
left_join(data$smth_smth %>%
mutate(grp = "Gesamt") %>%
select(vdat_year, grp, perc),
by = c("year" = "vdat_year", "grp")
) %>%
group_by(grp) %>%
mutate(across(c(n_pers, vyears), ~ (.x - lag(.x)) / lag(.x),
.names = "delta_{.col}"
)) %>%
select(
grp, year, n_pers, delta_n_pers, vyears, delta_vyears,
n_d, perc
)
vs "cleaner" version
x <-
x %>%
mutate(
vyears =
case_when(
year == 2014 ~ vdays / 365,
year == 2013 ~ vdays / 365,
TRUE ~ NA_real_
),
vyears = as.integer(round(vyears))
) %>%
select(-vdays) %>%
relocate(vyears, .after = n_pers)
# prepare join
y_join <-
data$am_percent %>%
mutate(grp = "Gesamt") %>%
select(vdat_year, grp, perc)
# Join Data
x <-
left_join(x, y_join,
by = c("year" = "vdat_year", "grp")
) %>%
group_by(grp) %>%
mutate(across(c(n_pers, vyears), ~ (.x - lag(.x)) / lag(.x),
.names = "delta_{.col}"
)) %>%
select(
grp, year, n_pers, delta_n_pers, vyears, delta_vyears,
n_d, perc
)