Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data.table::rleid for dplyr #1534

Closed
ghost opened this issue Nov 14, 2015 · 2 comments
Closed

data.table::rleid for dplyr #1534

ghost opened this issue Nov 14, 2015 · 2 comments

Comments

@ghost
Copy link

ghost commented Nov 14, 2015

Hi

it would be cool to have data.table::rleid in dplyr for grouping, window functions etc.

http://stackoverflow.com/questions/33507868/is-there-a-dplyr-equivalent-to-data-tablerleid

myrleid <- function(x) {
x <- rle(x)$lengths
rep(seq_along(x), times=x)
}

DT <- data.frame(grp=rep(c("A", "B", "C", "A", "B"), c(2,2,3,1,2)), value=1:10)
DT %<>% mutate(rlid = myrleid(grp))
DT

Greetings
Christof

@hadley
Copy link
Member

hadley commented Mar 1, 2016

group_indices() ?

@hadley hadley closed this as completed Mar 1, 2016
@yutannihilation
Copy link
Member

yutannihilation commented Aug 30, 2017

Just for future reference, group_indices() is NOT equivalent to data.table::rleid().

For example, suppose we want to treat first three "a" and last "a" in the data bellow are different groups:

d <- data.frame(x = c("a", "a", "a", "b", "b", "a"), stringsAsFactors = FALSE)

dplyr::group_indices(d, x)
#> [1] 1 1 1 2 2 1

data.table::rleid(d$x)
#> [1] 1 1 1 2 2 3

This can be implemented easily like this:

my_rleid <- function(x) {
  1L + cumsum(dplyr::coalesce(x != dplyr::lag(x), FALSE))
}

(But, I think this is not dplyr's job. Maybe tidyr can fit since this is the similar problem which functions like fill() try to solve in that they both are about how to work with consecutive rows.)

@lock lock bot locked as resolved and limited conversation to collaborators Jun 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants