1

I have the following example data. I would like to lookup each of the values of the input_vector and match those corresponding to letters from xxx. So the output should be a character vector.

The catch is that I would like this to be vectorized and the length and order of the returned vector should always match to the input_vector. I am sure this seems very simple but I would like to avoid using ifelse as i have many more than 26 numbers/characters - in fact I am after the fastest piece of code, irrespective of dependencies. Obviously a plus if I can use data.table.

nmbr<- 1:26
xxx <- data.table(data.frame(letters, nmbr), stringsAsFactors=FALSE)
input_vector<- setDT(as.data.table(round(runif(100, 1, 26))))
J. Doe.
  • 1,255
  • 1
  • 12
  • 25
  • If you would specify example data for `letters` and `nmbr`, it will be a lot easier for others to help you. – Jaap Apr 11 '18 at 08:41
  • Sorry, yes. `letters` is already built in R and `nmbr` is simply 1:26 – J. Doe. Apr 11 '18 at 08:41
  • Adding to that: (1) you can just use `as.data.table` instead of `setDT(as.data.table`; (2) similarly, just `data.table(letters, nmbr)` is also enough and (3) when generating random values, use `set.seed`; then others will be able to reproduce your data. – Jaap Apr 11 '18 at 08:45
  • `input_vector[xxx, on = .(V1 = nmbr), lets := i.letters][]` should do it – Jaap Apr 11 '18 at 08:47
  • Thank you @Jaap . If you could just post it as an answer that would be great. – J. Doe. Apr 11 '18 at 09:30
  • It is similar to the answers in the duplicate links. So we can leave it like it is now imo. Your question will then function as a reference point to the other, more elaborate, Q&A's. – Jaap Apr 11 '18 at 09:37

0 Answers0