A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/tidyverse/dplyr/issues/3760 below:

GitHub ยท Where software is built

A common case is that one constructs a grouping variable in group_by but only needs it for the duration of the group_by so afterwards one must use select to get rid of it as in the example below. It would be pleasingly symmetric if ungroup could remove the added column just as group_by adds it so

would be the same as

Thus in this example taken from https://stackoverflow.com/questions/51939874/referencing-previous-column-value-as-column-is-created/51940343#51940343

test <- structure(list(i = c(0, 1, 2, 3, 4, 0, 1, 2, 3, 4), chng = c(0, 
0.031, 0.005, -0.005, 0.017, 0, 0.012, 0.003, -0.013, -0.005), 
    indx = c(1, 1.031, 1.037, 1.031, 1.048, 1, 1.012, 1.015, 
    1.002, 0.997)), class = "data.frame", row.names = c(NA, -10L
))

test %>%
  group_by(g = cumsum(i == 0)) %>%
  mutate(indx = cumprod(chng + 1)) %>%
  ungroup %>%
  select(-g)

we could write using one fewer statement, i.e. the last two lines of code above are combined into the last line below.

test %>%
  group_by(g = cumsum(i == 0)) %>%
  mutate(indx = cumprod(chng + 1)) %>%
  ungroup(-g)

Note the reduced line count and improved symmetry.

j450h1, maxmoro, calebbraun and djbirke


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4