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

2011-03-04 Thread Greg Snow
011 6:52 AM > To: d.rizopou...@erasmusmc.nl > Cc: r-help@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 w

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

2011-03-04 Thread drflxms
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 D

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

2011-03-04 Thread Ivan Calandra
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 co

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

2011-03-04 Thread 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

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

2011-03-04 Thread drflxms
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