Create a numeric vector with names in one statement?

前端 未结 6 1934
无人及你
无人及你 2020-12-01 09:52

I\'m trying to set the default value for a function parameter to a named numeric. Is there a way to create one in a single statement? I checked ?numeric and ?vector but it

6条回答
  •  被撕碎了的回忆
    2020-12-01 10:48

    The convention for naming vector elements is the same as with lists:

    newfunc <- function(A=1, B=2) { body}  # the parameters are an 'alist' with two items
    

    If instead you wanted this to be a parameter that was a named vector (the sort of function that would handle arguments supplied by apply):

    newfunc <- function(params =c(A=1, B=2) ) { body} # a vector wtih two elements
    

    If instead you wanted this to be a parameter that was a named list:

    newfunc <- function(params =list(A=1, B=2) ) { body} 
        # a single parameter (with two elements in a list structure
    

提交回复
热议问题