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 8736703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:21:38+00:00 2026-06-13T10:21:38+00:00

for example: method1 <- function(alpha,beta,ff) Does R allow this ??? What I am try

  • 0

for example:

method1 <- function(alpha,beta,ff)

Does R allow this ???

What I am try to achieve is that: I have a general method, method1. And I have other methods: f2,f3,f4. I want it to be like method1 <- function(a,b, ff), where a & b are constants and most importantly, ff can be either f2 or f3 or f4, depending on how i call the function on the console. Ideally f2, f3, f4 computes a matrix.

Using the example in your answer below, I was wondering why cant I have this instead??

f1 <- function(a,b,ff(a,b))
{
    solve(ff(a,b))
}
f2 <- function (x,y){
    Diag(x*y)
}

This is a really bad example. But I would like to know why cant I include the ff(a,b) in the argument ??? What is the logic of writing ?

f1 <- function(a,b,ff){
ff(a,b)
}

  • 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-13T10:21:39+00:00Added an answer on June 13, 2026 at 10:21 am

    Depends what you mean. You can pass a function as an argument to another function, and you can make the default argument a function:

    f <- function(a,b,f2=function(x,y) { x+y}) {
       f2(a,b)
    }
    f(1,2) ## 3
    

    You can pass the results of a function call:

    f <- function(a,b,c) { a + b + c }
    f3 <- function(x,y) { x*y }
    f(1,2,f3(3,4))
    

    But you can’t do exactly what you specified: arguments must be named according to the standard variable-naming conventions (see ?make.names).

    f <- function(a,b,f2(1,3)) { ... ## error
    

    You can do it by using backticks, or by assigning names(formals(f))[3] <- "f2(1,3)", but that’s dangerous and probably not what you wanted.

    function(a,b,`f2(1,3)`) { }
    

    If you explain a little more of the context of what you’re trying to do you might get a more meaningful answer …

    edit: taking a guess based on your description, you want the first thing I described above.

    f1 <- function(a,b,ff) {
       ff(a,b) 
    }
    f2 <- function(x,y) diag(x)*y
    f3 <- function(x,y) matrix(runif(x*y),nrow=x,ncol=y)
    f4 <- function(x,y) outer(seq(x),seq(y))
    f1(2,2,f2)
    ##      [,1] [,2]
    ## [1,]    2    0
    ## [2,]    0    2
    f1(2,2,f3)
    ##             [,1]       [,2]
    ## [1,] 0.773723089 0.09640121
    ## [2,] 0.006206349 0.84351541
    f1(2,2,f4)
    ##      [,1] [,2]
    ## [1,]    1    2
    ## [2,]    2    4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example: I have a method -myFooBarMethod:withFoo:bar:moreFoo: and inside the implementation of that method I
I have a DrawingVisual object I have created for example using this method: Private
is there any example on using java method to call rhino-javascript function and return
I have an example class public class MyClass{ ActionResult Method1(){ .... } [Authorize] ActionResult
For example record like this: property3->property2->property1 is understood for me, this means that property
In Chapter 9 of Programming In Scala, there is an example method like this:
For example, I have a class that has two strings, one of which must
I have a javascript function that reformats a link. When a HREF link is
I was wondering if its possible to use Javascript to change the function/method that
I have a state machine that needs to call a different method on each

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.