Create a numeric vector with names in one statement?

R

R Problem Overview


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 doesn't seem so. Perhaps I can convert/coerce a matrix or data.frame and achieve the same result in one statement? To be clear, I'm trying to do the following in one shot:

test = c( 1 , 2 )
names( test ) = c( "A" , "B" )

R Solutions


Solution 1 - R

The setNames() function is made for this purpose. As described in Advanced R and ?setNames:

test <- setNames(c(1, 2), c("A", "B"))

Solution 2 - R

...as a side note, the structure function allows you to set ALL attributes, not just names:

structure(1:10, names=letters[1:10], foo="bar", class="myclass")

Which would produce

 a  b  c  d  e  f  g  h  i  j 
 1  2  3  4  5  6  7  8  9 10 
attr(,"foo")
[1] "bar"
attr(,"class")
[1] "myclass"

Solution 3 - R

How about:

 c(A = 1, B = 2)
A B 
1 2 

Solution 4 - R

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

Solution 5 - R

magrittr offers a nice and clean solution.

result = c(1,2) %>% set_names(c("A", "B"))
print(result)
A B 
1 2

You can also use it to transform data.frames into vectors.

df = data.frame(value=1:10, label=letters[1:10])
vec = extract2(df, 'value') %>% set_names(df$label)
vec
 a  b  c  d  e  f  g  h  i  j 
 1  2  3  4  5  6  7  8  9 10
df
    value label
 1      1     a
 2      2     b
 3      3     c
 4      4     d
 5      5     e
 6      6     f
 7      7     g
 8      8     h
 9      9     i
 10    10     j

Solution 6 - R

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  

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSurajView Question on Stackoverflow
Solution 1 - RAaron SchumacherView Answer on Stackoverflow
Solution 2 - RTommyView Answer on Stackoverflow
Solution 3 - RjoranView Answer on Stackoverflow
Solution 4 - RIRTFMView Answer on Stackoverflow
Solution 5 - RSebastianView Answer on Stackoverflow
Solution 6 - RKarl BakerView Answer on Stackoverflow