How to extract a number into digits using R?

user2717901 picture user2717901 · Aug 26, 2013 · Viewed 8.8k times · Source

Suppose I have a number: 4321

and I want to extract it into digits: 4, 3, 2, 1

How do I do this?

Answer

blmoore picture blmoore · Aug 26, 2013

Alternatively, with strsplit:

x <- as.character(4321)
as.numeric(unlist(strsplit(x, "")))
[1] 4 3 2 1