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' .
cID
'cID'
'x1'
nvec = c(cID = num)
For a one-line solution, use setNames() :
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 .
setName()
Try using "["
> nvec <- numeric(0) > nvec[cID] <- num > nvec x1 1
I'm not sure if this is what you are asking for, but anyway
assign(cID, num)
means that
5 - x1
gives
[1] 4