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

From: Greg Snow <Greg.Snow_at_imail.org>
Date: Fri, 04 Mar 2011 11:39:21 -0700

Here is another way to do it without converting back and forth to character strings:

digits <- function(x) {

	if(length(x) > 1 ) {
		lapply(x, digits)
	} else {
		n <- nchar(x)
		rev( x %/% 10^seq(0, length.out=n) %% 10 )
	}

}

> digits(10010)

[1] 1 0 0 1 0
> digits( c(123, 4567, 273) )

[[1]]
[1] 1 2 3

[[2]]
[1] 4 5 6 7

[[3]]
[1] 2 7 3

This version only works on integers, so modification would be needed if you want to pass floating point numbers (but you would first need to decide what you would want the output to be).

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow_at_imail.org
801.408.8111



> -----Original Message-----
> From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-
> project.org] On Behalf Of drflxms
> Sent: Friday, March 04, 2011 6:52 AM
> To: d.rizopoulos_at_erasmusmc.nl
> Cc: r-help_at_r-project.org
> Subject: Re: [R] sum of digits or how to slice a number into its digits
>
> Hi Dimitris,
>
> thank you very much for your quick an efficient help! Your solution is
> perfect for me. Does exactly what I was looking for if combined with
> unlist and as.numeric before using sum.
>
> Now I can keep on with my real problem ;)...
> Thanx Again!!!
>
> Best, Felix
>
> Am 04.03.2011 14:25, schrieb Dimitris Rizopoulos:
> > one way is using function strsplit(), e.g.,
> >
> > x <- c("100100110", "1001001", "1101", "00101")
> > sapply(strsplit(x, ""), function (x) sum(x == 1))
> >
> >
> > I hope it helps.
> >
> > Best,
> > Dimitris
> >
> >
> > On 3/4/2011 2:18 PM, drflxms wrote:
> >> 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
> >>
> >> ______________________________________________
> >> R-help_at_r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide
> >> http://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
______________________________________________ R-help_at_r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Received on Fri 04 Mar 2011 - 18:41:55 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 04 Mar 2011 - 18:50:18 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive