Anonymous
Anonymous

Reputation: 17

Issue with If Condition in For Loop

I'm trying to find the largest number of people who did not survive in a dataframe that I am working on. I used a for loop to iterate through the rows but I'm having an issue. It doesn't seem like my if condition is working. It is saying that the largest number is 89 but it is actually 670.

most_lost <- 0
    for (i in 1:dim(Titanic)[1]) {
      if (Titanic$Survived[i] == "No")  {
        if (Titanic$Freq[i] > most_lost) {
          most_lost <- Titanic$Freq[i]
        }
        print(most_lost)
      }
    }

This is the output of the printed most_lost

[1] 0
[1] 0
[1] "35"
[1] "35"
[1] "35"
[1] "35"
[1] "35"
[1] "35"
[1] "35"
[1] "35"
[1] "387"
[1] "670"
[1] "670"
[1] "670"
[1] "89"
[1] "89"

Here is the table I'm working with

enter image description here

Upvotes: 0

Views: 55

Answers (2)

Travis Hinkelman
Travis Hinkelman

Reputation: 164

If I'm understanding correctly, you don't need a for loop.

max(Titanic$Freq[Titanic$Survived == "No"])

This line is subsetting the Freq column by rows where the Survived column is "No" and then finding the max value of the subsetted Freq column.

Upvotes: 0

Manuel Bickel
Manuel Bickel

Reputation: 2206

Could you please check the data formats in your table, e.g., is Freq really numeric? With below example data your code works for me - see below code. As a side note, it would be better if you would not post your data as a figure, use, e.g., dput(data) instead and post its output, this makes it easier for others to import your data and check its structure. You might edit your question accordingly.

In any case, I would like to highlight, that for the task you describe you should not use a loop but simply subset your table, since looping will be unacceptably slow for such tasks with larger data sets. I have provided an example at the end of below code.

Titanic = as.data.frame(cbind(Survived = rep("No", 8), Freq = c(1,2,5,0,2,3,1,1)), stringsAsFactors = F)
#   Survived Freq
# 1       No    1
# 2       No    2
# 3       No    5
# 4       No    1
# 5       No    2
# 6       No    3
# 7       No    1
# 8       No    1
most_lost <- 0
for (i in 1:dim(Titanic)[1]) {
  if (Titanic$Survived[i] == "No")  {
    if (Titanic$Freq[i] > most_lost) {
      most_lost <- Titanic$Freq[i]
    }
    print(most_lost)
  }
}
# [1] "1"
# [1] "2"
# [1] "5"
# [1] "5"
# [1] "5"
# [1] "5"
# [1] "5"
# [1] "5"

max(Titanic[Titanic$Survived == "No", "Freq"])
# [1] "5"

Upvotes: 3

Related Questions