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

  • Home
  • SEARCH
  • 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 8192949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:18:34+00:00 2026-06-07T04:18:34+00:00

Is it possible to chain functions in R? Sample data: m <- matrix(c(1:10, 11:20),

  • 0

Is it possible to chain functions in R?

Sample data:

m <- matrix(c(1:10, 11:20), nrow = 10, ncol = 2)

For example, I would like to replace the following statements below:

step1 <- mean(m)
step2 <- sum(step1)
res <- step2

Or,

res <- sum(mean(m))

With something like this :

res <- m@mean()@sum()

In some cases, that would clarify my code considerably.

EDIT1
This is a dummy example. I randomly picked ‘sum’ and ‘mean’.

Ben has given a first piece of answer using %@% however, it prevents from using extra arguments within functions :

m %@% function1(arg1, arg2) %@% function2(arg1, arg2)

How can I work around that ?

EDIT2
Adding an example

require(xts)
require(PerformanceAnalytics)
xts.ts <- xts(rnorm(231),as.Date(13514:13744,origin="1970-01-01"))
plot(na.omit(lag( rollapply(xts.ts, width=rolling.per-1, FUN= function(x){sqrt(var(x))*sqrt(252)}), k=1)), main = "Dummy Example")

This example seems to work fine with Charles solution :

`%@%` <- function(x, f) eval.parent(as.call(append(as.list(substitute(f)), list(x), 1)))
xts.ts %@% rollapply( width = rolling.per-1, FUN= function(x) x%@%var%@%sqrt * sqrt(252) ) %@% lag( k=1) %@% na.omit %@% plot(main = "Dummy Example")

Less important to my case, but woth mentioning, the following statment fails with Charles’s solution :

 xts.ts %@% names <- 'ts name' 
  • 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-07T04:18:37+00:00Added an answer on June 7, 2026 at 4:18 am

    In a similar vein to Ben’s answer, but allowing arguments:

    `%@%` <- function(x, f) eval.parent(as.call(append(as.list(substitute(f)), list(x), 1)))
    
    x %@% mean %@% sqr # => 6.25
    c(1, 2, NA, 3, 4) %@% mean(na.rm=T) %@% sqr # => 6.25
    m %@% colMeans() %@% sum() # => 21
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to 'chain' functions? Say, I have a serverclickhandler and then would
Is it possible in jQuery to daisy-chain selectors like so? var sData = $('#myTableRow
Is it possible to chain setTimout functions to ensure they run after one another?
Is it possible that there are more than one certificate chain built during the
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Would it be possible to create a python Pool that is non-daemonic? I want
I would like to have a function that returns something like an array of
I was wondering if it is possible in C (89/90) to chain function calls,
Does someone knows if it's possible to dynamically create a call chain and invoke
Possible Duplicate: Can a JavaScript object have a prototype chain, but also be a

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.