Skip to content

what 'kind' of func_factory to use for multiple argument grouped non-aggregating function? #151

Answered by pwwang
ftobin asked this question in Q&A
Discussion options

You must be logged in to vote

Sorry for the late response. I was off for a couple of days.

This should be apply_df for sure. So the solution will be:

from datar import f
from datar.core.factory import func_factory
from datar.core.tibble import TibbleGrouped
from datar.dplyr import group_by, mutate
from datar.datasets import mtcars

# register the generic function
mymult = func_factory({"x", "w"}, kind="apply_df", func=lambda x, w: ...)

# .register() provides post hook
# The index of the result is problematic for mutate()
@mymult.register(TibbleGrouped, post=lambda out, x, w: out.reset_index(drop=True))
def mymult(x, w):  # Ideally I could say w=1
    return x * w

mtcars >> group_by(f.cyl) >> mutate(x = mymult(f.disp, f

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@ftobin
Comment options

@pwwang
Comment options

@ftobin
Comment options

Answer selected by ftobin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants