Digit count

What is the better way to count the number of digit in a number? digit_count recursively divide the number by its base (10) cast it into string and use str_length to count the number of characters digit_count <- function(number,count=1) { if (number < 10) {return(count)} return(digit_count(number %/% 10, count=count+1)) } Definitions a = function(x) {return(digit_count(x))} b = function(x) {return(stringr::str_length(as.character(1000)))} Timing

Continue reading

apply() function

How it works Example Data This is the value of matrix a ## [,1] [,2] [,3] ## A 1 6 11 ## B 2 7 12 ## C 3 8 13 ## D 4 9 14 ## E 5 10 15 Operation with built-in functions The mean of a’s rows apply(a,1, mean) ## A B C D E ## 6 7 8 9 10 The stddev of a’s rows

Continue reading

Author's picture

Trang Tran


Student

USA