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

Port groupby() from Python #7

Open
ramhiser opened this issue May 9, 2014 · 3 comments
Open

Port groupby() from Python #7

ramhiser opened this issue May 9, 2014 · 3 comments
Milestone

Comments

@ramhiser
Copy link
Owner

ramhiser commented May 9, 2014

While this is an interesting function, dplyr handles this case well. Also, groupby may not translate easily to R because of the lack of dictionaries.

@ramhiser
Copy link
Owner Author

It turns out that Python's groupby is very similar to how R's tapply behaves. The main differences are that Python's groupby:

  1. Iterates with a key-value pair, similar to enumerate
  2. Requires the iterable to be sorted by the key beforehand.

Difference 2 is especially important because otherwise, the iterator needs to be consumed before each group's data (i.e., factor) can be seen and processed.

@ramhiser
Copy link
Owner Author

A couple of examples where groupby is useful:

  1. Averaging over continuous blocks
  2. Partial means of vector elements

The second entry is marked as a duplicate of the first, but groupby provides an alternative solution using iterators.

@ramhiser
Copy link
Owner Author

Also, code up an iterator version of Run Length Encoding from base::rle().

Example usage:

> x <- c(rep(6:10, 1:5), 6)
> x
 [1]  6  7  7  8  8  8  9  9  9  9 10 10 10 10 10  6
> rle(x)
Run Length Encoding
  lengths: int [1:6] 1 2 3 4 5 1
  values : num [1:6] 6 7 8 9 10 6

@ramhiser ramhiser modified the milestones: Version 0.1.3, Version 0.1.2 Jan 17, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant