How can I pass multiple arguments to a function as a single vector?
Try this
do.call(nDone, as.list(d))
Explanation of what's happening in your first attempt by @joran from the comments:
R is seeing you pass a single argument to nDone
, namely the vector d
, which is handed off to the first function argument, under
. Since you haven't specified a default value for the others, they are missing and hence the error
Apply function with multiple arguments to a vector in Julia
Use broadcast - just as you suggested in the question:
julia> f = function(a,b)
a*b
end
#1 (generic function with 1 method)
julia> x=[1,2,3,4,5]
5-element Vector{Int64}:
1
2
3
4
5
julia> b=3
3
julia> f.(x, b)
5-element Vector{Int64}:
3
6
9
12
15
map
does not broadcast, so if b
is a scalar you would manually need to write:
julia> map(f, x, Iterators.repeated(b, length(x)))
5-element Vector{Int64}:
3
6
9
12
15
You can, however, pass two iterables to map
without a problem:
julia> map(f, x, x)
5-element Vector{Int64}:
1
4
9
16
25
R apply function with multiple parameters
Just pass var2 as an extra argument to one of the apply functions.
mylist <- list(a=1,b=2,c=3)
myfxn <- function(var1,var2){
var1*var2
}
var2 <- 2
sapply(mylist,myfxn,var2=var2)
This passes the same var2
to every call of myfxn
. If instead you want each call of myfxn
to get the 1st/2nd/3rd/etc. element of both mylist
and var2
, then you're in mapply
's domain.
Elegant way to pass multiple arguments to a function
If all these parameters are meaningfully related, pack them in a structure.
Passing an entire group of variables as one argument to a function C++
Write a struct
with the parameters.
The multiple nested functions either need an override taking such a struct
, or be rewritten to consume it as their argument.
Now you can pass parameters around in a bundle.
If the set of parameters is not uniform, you might be out of luck. You also might be able to solve it using inheritance and the like. It will depend a lot on details.
If there is a significant set of parameters that is uniform, making them a struct
can reduce the number of parameters you pass around.
void do_math(int x, int y, int z, int w);
vs
struct coords {
int x,y,z,w;
};
void do_math(coords);
if we want to keep the original do_math
around we can:
inline void do_math(coords c) { do_math(c.x, c.y, c.z, c.w); }
now we can have code that wants to call do_math
a bunch of times with tweaks;
void do_math_over_and_over(coords c, int x_range) {
for(int i = 0; i < x_range; ++i) {
coords tmp = c;
tmp.x += i;
do_math(tmp)
}
}
send multiple arguments using apply function
Assuming you want to pass the row, and a single argument that is the same for each row:
manydo3 <- function(x, r1) NULL
apply(eq, 1, manydo3, r1=18)
If you want different values for the second argument for each row, then you want to split your matrix into rows and pass both the rows and your other argument with mapply
:
mapply(manydo3, split(eq, row(eq)), R)
where length(R) == nrow(eq)
(i.e. R contains r1, r2, etc).
How to use a vector as argument of a function with multiple parameters in r
You can use the sapply()
inside your function
fun1 <- function (a, b, c, d) {
sapply(d, function(x) { sum (( a - x ) * b / c ) })
}
fun1(df$a, df$b, nrow(df), c(1:3))
# [1] 2696.223 2643.748 2591.273
Related Topics
Warning in Install.Packages: Unable to Move Temporary Installation
Remove 'Search' Option But Leave 'Search Columns' Option
Legend of a Raster Map with Categorical Data
Shiny Dashboard - Display a Dedicated "Loading.." Page Until Initial Loading of the Data Is Done
R Grep Pattern Regex with Brackets
Add Column Containing Data Frame Name to a List of Data Frames
Spacing Between Boxplots in Ggplot2
How to Merge Two Data Frames on Common Columns in R with Sum of Others
Stop Lapply from Printing to Console
Ternary Plot and Filled Contour
Represent Numeric Value with Typical Dollar Amount Format
Union of Intersecting Vectors in a List in R
Data.Table Package in R 3.5 Does Not Install
Reshape Multi Id Repeated Variable Readings from Long to Wide