4
want to construct a function that can have the number of variable parameters, as the function c
, and how to access their addresses, for example:
c(a = 1, b = 2, d = 9)
#acessar a, b e d
Has something to do with ...
? What do they mean?
4
want to construct a function that can have the number of variable parameters, as the function c
, and how to access their addresses, for example:
c(a = 1, b = 2, d = 9)
#acessar a, b e d
Has something to do with ...
? What do they mean?
7
dot-dot-dot
or ...
is called ellipse.
minha_funcao_com_elipse <- function(...) {
input_list <- list(...)
input_list
}
See the result of this function:
> minha_funcao_com_elipse(a = 1, b= 2, c= 3)
$a
[1] 1
$b
[1] 2
$c
[1] 3
Note that the command input_list <- list(...)
creates a list of all parameters that have been passed within the ellipse.
Like input_list
is a common list of R, it is easy to access its elements. The function c
, for example could be imitated as follows:
> c2 <- function(...) {
+ unlist(list(...))
+ }
> c2(a = 1, b= 2, c= 3, 5, 6)
a b c
1 2 3 5 6
Is it worth reading this response from the OS. Another good reference is the Advanced R in the session ...
.
6
You can define a function using the ...
, which can be used to access arguments explicitly passed by name. To access the values, you convert the ...
in a list (using list(...)
). The code below shows an example:
c <- function(...) {
args <- list(...)
argNames <- names(args)
print(argNames)
sum(sapply(args, function(x) x))
}
c(a = 1, b = 4, d = 9)
Browser other questions tagged r
You are not signed in. Login or sign up in order to post.