I have a question. My main problem with reading solutions online is that I cannot understand 95% of them because the answers are usually too technical.
Question: I have created a basic function that returns one answer. Now I need to make this same function repeats itself for N/2 times and returns N/2 answers.
The base function is as per below. User inputs the function with a variable N. Assuming he inputs N=100, the function generates list a, list b and list c. Then the function counts how many times a criteria is achieved and returns the count value. In the base case, list b is created with the first 10 elements of list a and list c is created with the remaining N-10 elements.
f <- function (N) {
count <- 0
for (nn in 1:100) {
a <- sample(1:N)
b <- a [1:10]
c <- a [11:N]
pass <- which(c>=max(b))
if (length(pass) == 1) {
count <- count + 1
}
}
return (count)
}
answer <- f(100)
answer
The recursive function has to make this base function repeats N/2 times. The splitting of list b and c is no longer based on 10 elements and (N-10) elements. It is now based on 1) 1, N-1 elements, then 2) 2, N-2 elements, then 3) 3, N-3 elements and so on until the N/2 case (which is both lists are split evenly).
As such I tried this but it does not work. I need a recursive function that can generate N/2 answers which is one answer for each of the cases. As I am really new to Programming, I hope the suggestions can be explicit. I may not have the ability to understand implicit and partial codes.
Thank you everyone for being so kind and taking the time to help others.
f <- function (N) {
for (mm in 1: N/2) {
number <- list()
count <- 0
for (nn in 1:100) {
a <- sample(1:N)
b <- a [1:mm]
c <- a [mm+1:N]
pass <- which(c>=max(b))
if (length(pass) >= 1) {
count <- count + 1
}
}
number[mm] <- count
}
}
answer <- f(100)
answer
# NULL
The latest edited code that a helpful commenter (Oliver) has come up with is as per below. But it is only working for one case. Basically the function attempts to slice a list of 100 random numbers into 2 parts, b and c. Then it attempts to see if c contains exactly one number that is higher than the highest number in b. The for-loop (mm in 1:(N/2)) simulates a continuous slicing action for b and c. It starts with 1 and ends with 50 (N/2). When it is 1, b has 1 element and c has 100-1 elements, when it is 2, b has 2 elements and c has 98 elements... with the last case being b and c both having 50 elements.
f <- function (N) {
for (mm in 1: (N/2)) {
number <- list()
count <- 0
for (nn in 1:100) {
a <- sample(1:N)
b <- a [1:mm]
c <- a [mm+1:N]
pass <- which(c>=max(b))
if (length(pass) == 1) {
count <- count + 1
}
}
number[mm] <- count
}
number
}
bb <- f(100)
bb
[[46]]
NULL
[[47]]
NULL
[[48]]
NULL
[[49]]
NULL
[[50]]
[1] 26
If the code works correctly, the expected answers for [46], [47], [48] and [49] should be 20+ and not NULL. I believe the code for number[mm] <- count is not working as intended. I believe number[mm] is not being successfully transmitted to the final code number except for number[50] which is transmitted correctly.
Welcome to SO, thank you for a well formulated question.
First things straight. Recursion refers to the calling of a function within the same function as in the (not very explicit) example below:
f <- function(x){
some code
n <- f(x2)
some more code
return(x3)
}
Now for your problem. You're close to the answer you want. Three things got messed up in the process,
number at each iteration.For 1. R reads code left to right, and will be very strict in how it calculates your code. 1:N/2 is equivalent to 1: N/2 which is equivalent to c(1, 2, ..., N) / 2. You're looking for 1:(N/2) or seq(1, N/2), and you likely would like to use either floor or ceiling for rounding uneven results of N/2, such as 5/2=2.5, 13/2=6.5 etc.
For 2. you are currently not returning anything from your function. In R you can explicitly return a value using return(...), or you can simply type out the object/number as the last thing in your function.
For 3, note that in the outer loop, you are calling number <- list(). At each iteration of the outer loop, this will reset the list of counts, thus removing any previous answer. This should be moved
Putting these together, getting the piece of code right amounts to the example below:
f <- function (N) {
number <- list() # <=== initiate your canister only once.
for (mm in 1:(N/2)) { #<=== remember parenthesis. Or use seq(1, N/2)
count <- 0
for (nn in 1:100) {
a <- sample(1:N)
b <- a [1:mm]
c <- a [mm+1:N]
pass <- which(c>=max(b))
if (length(pass) >= 1) {
count <- count + 1
}
}
number[mm] <- count
}
number #<== or return(number)
}
I wouldn't mind giving a few general suggestions, but it isn't quite clear to me, what the greater picture of the function is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With