the function ends earlier then it is desired

Hello, I am trying to set up a function that will give me a sum of something, but it didn't work out and the output seemed not what I want.
I am conjecturing that it is because I used another function to set up the main function. I know it is possible that once I get to a return or print then everything else behind won't be executed. But I don't know how to avoid the necessary return since it is used to construct my function.

prob.a<-function(N){
for(j in 1:9){
 	k <- rep(1:N, times=N)
 	i <- rep(1:N, each=N)
 	n <- i <= k
	count <- rep(0,9) #initiate counter
 	alpha <- function(p) {
 		for(d in 1:p) {
 			m=first_digit(d)
 			count[m]=count[m]+1 #counting
 		}
 		print(count[j]) #the result of counting
 		}
 	result<-sum(alpha(length(i[n]))/(k[n]*N)) #desirous result of prob.a function
	return(result)
}
}

And I try this function by giving N=3:

> prob.a(3)
[1] 1
[1] 1

The result shows that the loop is not working, does anyone know what causes that? Thanks a lot.

Use
debug(prob.a)
To set up debugging mode. Then call your function, and step through it to see how it works from command to command.

There are a couple of things which can be improved, reorganized or are not needed at all.

For instance, the return statement is not needed as it's the last statement of the prob.a function and it will suffice to just use result as your last statement and that would be the same for the print statement.
Also, the assignment of the k and i variable is done every time when the for statement is executed. I would place it above the for statement.
Another improvement is to place the definition of the alpha function outside (above) the prob.a function, otherwise you keep defining it again and again when executing the for loop.

However, the following issue is breaking the whole setup anyway: during debugging , I got the error could not find function "first_digit". So where is this function coming from? Self defined or from a specific package?

Sorry for not explaining explicitly, first_digit is indeed a self-defined function. I did not show that in the script above. It is given by:

first_digit <- function(x){
  floor(x*10^(-floor(log10(x))))
}

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!

can you provide an english/mathematical explanation of what calculation you are wanting to do ?
also, it would be good to have a simple example of inputs and expected outputs for 1 or 2 cases

Screenshot 2021-03-22 at 11.27.11 PM
I want to write this as a function of N
where alpha_i(j) stands for the no. of the numbers (1:i) that the first digit is j.
For example,
alpha_3(1)=1
alpha_10(9)=1

Eventually, I want to plot the function over N=1~5000 for j=1:9.

based on your requirements for Alpha, I think you could use

first_digit <- function(x){
  floor(x*10^(-floor(log10(x))))
}

#where alpha_i(j) stands for the no. of the numbers (1:i) that the first digit is j.#
alpha <- Vectorize(function(i,j){
  sum(first_digit(1:i)==j)
})

#e.g. one at a time analysis
alpha(1,1)
alpha(5,1)
alpha(50,1)
alpha(100,1)
# or do a set of analysis
alpha(1:100,1)

Thanks for your kind reply, I was not really familiar with Vectorize before. I'll use this to continue my work. Big thanks!!

Just to say that only i is effectively parameterised in this construction, but I believe that's sufficient

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.