Name of attribute variable on named vector

I have a string and a number

cID = 'x1' num = 1 

I want to create a named vector

 nvec = c(x1 = num) 

but when I do the following, R interprets cID as 'cID' and not as 'x1' .

 nvec = c(cID = num) 
+4
source share
3 answers

For a one-line solution, use setNames() :

 nvec <- setNames(num, cID) nvec # x1 # 1 

In the example where setName() provided a clean and elegant solution to a complex problem, see @hadley for an answer to this question .

+6
source

Try using "["

 > nvec <- numeric(0) > nvec[cID] <- num > nvec x1 1 
+3
source

I'm not sure if this is what you are asking for, but anyway

 assign(cID, num) 

means that

 5 - x1 

gives

 [1] 4 
0
source

All Articles