-1

I want to create a numeric vector in R with a placeholder. Just like in a chracter vector like:

characterVec <- c("a", "b", "", "d")

This gives me a characterVec vector with a length of 4.

How can I create a numeric vector with a length of 4, but still has one empty value? For example, I would like to know what do I put into the question mark in the following vector.

numericVec <- c(1, 2, ?, 4)
AT90
  • 51
  • 7

1 Answers1

0

If I'm understanding your question properly, you can use a named vector to create a data dictionary linking letters to corresponding numbers:

# data dictionary
dat <- 1:26
names(dat) <- letters

then map dictionary onto your vector

characterVec <- c("a", "b", "", "d")
numVec <- dat[characterVec]

gives

   a    b <NA>    d 
   1    2   NA    4 

You can remove the vector names with unname():

numVec <- unname(dat[characterVec])
Paul
  • 2,877
  • 1
  • 12
  • 28