Create a numeric vector with names in one statement?

前端 未结 6 1931
无人及你
无人及你 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:33

    To expand upon @joran's answer (I couldn't get this to format correctly as a comment): If the named vector is assigned to a variable, the values of A and B are accessed via subsetting using the [ function. Use the names to subset the vector the same way you might use the index number to subset:

    my_vector = c(A = 1, B = 2)    
    my_vector["A"] # subset by name  
    # A  
    # 1  
    my_vector[1] # subset by index  
    # A  
    # 1  
    

提交回复
热议问题