Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8140001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:01:03+00:00 2026-06-06T12:01:03+00:00

I have been defined a bind function , b <- function(f,…) function(x) f(x, …)

  • 0

I have been defined a bind function ,

b <- function(f,...) function(x) f(x, ...)

so I can do things like (this is a fake example)

d = data.frame(x=c(1,2,1), y=c(10, 20, 5))
ddply(d, ~x, b(transform, y=sum(y)))

instead of

ddply(d, ~x, function (df) { transform(df, y=sum(y)) }

Now, I’m trying to define an operator

'%b%' <- function(x,...) b(x,...)

and try

ddply(d, ~x, transform %b% (z=y*10)))

It doesn’t work. What is the difference ?

When I do

> b(transform, y=y/sum(y))(d)
x         y c.1..2..1.
1 1 0.2857143          1
2 2 0.5714286          2
3 1 0.1428571          1

That works, but

> transform %b% (y=y/sum(y))(d)
Error in transform %b% (y = y/sum(y))(d) : object 'y' not found

I understand, there is a ‘capture’ difference, what can I do ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:01:05+00:00Added an answer on June 6, 2026 at 12:01 pm

    To make your example work, you need to match the function argument to an existing function, using match.fun():

    `%b%` <- function(x,...) match.fun(b)(x,...)
    ddply(d, ~x, transform %b% (z=y*10))
    
      x  y
    1 1 10
    2 1  5
    3 2 20
    

    It will also be safer if you use match.fun() in your first definition of f:

    b <- function(f, ...) function(x) match.fun(f)(x, ...)
    ddply(d, ~x, b(transform, y=sum(y)))
      x  y
    1 1 10
    2 1  5
    3 2 20
    

    Having answered your question, I now have to point out that I don’t understand why you want to do this, since the plyr functions like ddply as well as the base R apply functions already do this.

    So, I would write your original example simply like this:

    ddply(d, ~x, transform, y=sum(y))
      x  y
    1 1 15
    2 1 15
    3 2 20
    
    ddply(d, ~x, transform, y=y/sum(y))
      x         y
    1 1 0.6666667
    2 1 0.3333333
    3 2 1.0000000
    

    Edit:

    I had another look at your question. You simply made a syntax error in your function definition. This works perfectly fine with an infix operator:

    '%b%' <- function(f,...) function(x) f(x, ...)
    ddply(d, ~x, transform %b% (z=y*10))
      x  y
    1 1 10
    2 1  5
    3 2 20
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This has been driving me absolutely nuts. I have a substitute function like this:
How can I find the prototype methods (not PrototypeJS) that have been defined using
I have a connection protocol that has been defined by our customer. Data are
I need to find how many rows in worksheet have been defined. How can
I keep getting errors that my functions have been defined multiple times. Of course
I have been trying to assign value to an object array that is defined
I have an ItemsControl with a DataTemplate that has been defined. My ItemsControl definition
Have been following this fabulous tutorial . Being new to Javascript and functional programming,
have been searching for a mod operator in ios, just like the % in
Have been working on this question for a couple hours and have come close

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.