I tried to modify my script by referring to your suggestions but still, some problems remained confusing to me.
Currently, my script is being:
first_digit <- function(x){
floor(x*10^(-floor(log10(x))))
}
count <- rep(0,9) #initiate counter
alpha <- function(p) {
for(d in 1:p) {
m=first_digit(d)
count[m]=count[m]+1 #counting
}
return(count[j]) # for j=1:9 (need to add a loop)
}
prob.a<-function(N){
k <- rep(1:N, times=N)
i <- rep(1:N, each=N)
n <- i <= k
a <- alpha(#number i)
result <- sum(a/(k[n]*N)) # (sum over all (k,i) pairs) wanted result of prob.a function
print(result)
}
alpha(#number i) is where I got into trouble.
I know I cannot use i here since i is a vector.
I also tried length(i[n]) but obviously it is not what I want.
For another,
k <- rep(1:N, times=N)
i <- rep(1:N, each=N)
n <- i <= k
This is used to filter the (k,i) pairs which are used to calculate the sum (wanted result of prob.a).
So, for instance, if I give N=3, the first 3 lines will filter out (1,1) (2,1) (3,1) (2,2) (3,2) (3,3)
Then this should plug into (a/(k[n]*N) and sum up all 6 outcomes, then the output of the function prob.a will be this sum. My question is just I don't know how to correctly plug in the (k,i) pair I want.
Thanks for help again!