# Need help in for loop

I'm new to programming as well as R and I've written a for loop. I think when the run this code the result should be printed 6 times but instead it is printing 12 times.
Can someone help?
I've attached the code below👇

gender = c("male", "female","male", "female","male", "female")
length(gender)
for (i in 1:length(gender)) {
ifelse(gender=="male", print("M"), print("F"))
}
The output is:-
 "M"
 "F"
 "M"
 "F"
 "M"
 "F"
 "M"
 "F"
 "M"
 "F"
 "M"
 "F"

You have to iterate over the gender vector

gender = c("male", "female","male", "female","male", "female")
length(gender)
for (i in 1:length(gender)) {
ifelse(gender[i]=="male", print("M"), print("F"))
}

1 Like

To understand exactly why you get 12 values, it takes a bit of detective work. I'd say you actually have 3 potential problems in this code. The first one is what rafaelmenmell pointed out: if you use a `for` loop with index `i`, it's because you want to select the `i`th element of your vector at each occurrence of the loop.

But another potential problem, more an inefficiency, is that you're using `ifelse()`. If you use a for loop, a common way to write it would be:

``````for (i in 1:length(gender)) {
if(gender[i] == "male"){
print("M")
} else{
print("F")
}
}
``````

In that case, at each occurrence of the loop, you'd check the current value of `gender[i]` and act in consequence. But `ifelse()` is a very useful function, because it already contains a loop inside!

``````ifelse(gender=="male", "M", "F")
#>  "M" "F" "M" "F" "M" "F"
``````

So if you give the full vector to `ifelse()`, it will automatically loop over each element, so you don't need to write the loop yourself.

Finally a third problem, more on formatting, let's run your `ifelse()` command:

``````ifelse(gender=="male", print("M"), print("F"))
#>  "M"
#>  "F"
#>  "M" "F" "M" "F" "M" "F"
``````

That is a confusing result, but it makes sense. When R first sees `print("M")`, it doesn't think, it immediately prints it. Similarly, it then runs into `print("F")`, so prints it. Only then does it look at the actual command and run the `ifelse()` loop. So in your case, you don't need the `print()` inside the loop, you want to first run the loop without printing anything, then print the result. One way to make it explicit would be:

``````print(ifelse(gender=="male", "M", "F"))
#>  "M" "F" "M" "F" "M" "F"
``````

But note that when you run something in the R console, R assumes you want it printed (unless you tell it otherwise), so that `print()` is unnecessary.

One of the few cases where R doesn't automatically print a result is... in a `for` loop! So this code doesn't print anything:

``````for (i in 1:length(gender)) {
ifelse(gender=="male", "M", "F")
}
``````

Now we can look at why you get 12 results (and why they're wrong but you can't see it in that particular case). You define your `gender` vector of length 6, then run a for loop. So your loop will be run 6 times. Each time it's run, there is a `print("M")`, and a `print("F")`. So you get 6 times the output

``````#> M
#> F
``````

but what about the values inside the vector `gender`? Well, `ifelse()` does run on them, and computes the result, but since you're inside a for loop that result is not printed. So these values are actually completely ignored. You can see it with a different `gender`:

``````gender = c("male", "male","male", "female","female", "female")

length(gender)
for (i in 1:length(gender)) {
ifelse(gender=="male", print("M"), print("F"))
}
#>  "M"
#>  "F"
#>  "M"
#>  "F"
#>  "M"
#>  "F"
#>  "M"
#>  "F"
#>  "M"
#>  "F"
#>  "M"
#>  "F"
``````

oops, your example just happened to have perfectly alternating male and female, so it's a complete coincidence that the result looked correct. You can make it even more obvious by changing the number of times the loop runs:

``````gender = c("female", "male", "male", "female","female", "female", "male", "female","female", "female")

length(gender)
for (i in 1:2) {
ifelse(gender=="male", print("M"), print("F"))
}
#>  "M"
#>  "F"
#>  "M"
#>  "F"
``````

### Technical note

Actually when I wrote

When R first sees `print("M")`, it doesn't think, it immediately prints it. Similarly, it then runs into `print("F")`, so prints it. Only then does it look at the actual command and run the `ifelse()` loop.

I lied a bit to simplify. In practice, R will first run the `ifelse()` loop, then will try to give you back the results, and notice that the result has a `print()`, so will print it. It doesn't change my above explanation, but it changes that if you have `gender` which has only "male", the `ifelse()` will never run into `print("F")` so you will only print "M".

1 Like

This topic was automatically closed 42 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.