[R] sum of digits or how to slice a number into its digits

drflxms drflxms at googlemail.com
Fri Mar 4 14:18:03 CET 2011


Dear R colleagues,

I face a seemingly simple problem I couldn't find a solution for myself
so far:

I have to sum the digits of numbers. Example: 1010 ->2 100100110 -> 4
Unfortunately there seems not to be a function for this task. So my idea
was to use sum(x) for it. But I did not figure out how to slice a number
to a vector of its digits. Example (continued from above): 1010 ->
c(1,0,1,0)  100100110 -> (1,0,0,1,0,0,1,1,0).

Does anyone know either a function for calculating the sum of the digits
of a bumber, or how to slice a number into a vector of its digits as
described above?

I'd appreciate any kind of help very much!
Thanx in advance and greetings from cloudy Munich,
Felix



More information about the R-help mailing list