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
Dimitris Rizopoulos
2011-Mar-04 13:25 UTC
[R] sum of digits or how to slice a number into its digits
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. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/
Ivan Calandra
2011-Mar-04 13:27 UTC
[R] sum of digits or how to slice a number into its digits
Hi, Here is the best I've found: x <- 100100110 sum(as.numeric(unlist(strsplit(as.character(x), split="")))) It first converts x to character, then splits every character, unlist()s the results, then reconverts to numeric and sums it. HTH, Ivan Le 3/4/2011 14:18, drflxms a ?crit :> 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. >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. S?ugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php
Seemingly Similar Threads
- sorting a data.frame (df) by a vector (which is not contained in the df) - unexpected behaviour of match and factor
- selecting only corresponding categories from a confusion matrix
- percentile of a given value: is there a "reverse" quantile function?
- festival text for weather report
- colouring a table, data.frame or matrix in an interactive R session