If statements based on another column within a dataframe: in R <p>For a simple dataframe:</p> <pre><code>df &lt;- structure(list(id = 1:9, sex = structure(c(2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L), .Label = c("f", "m"), class = "factor"), score = c(55L, 60L, 62L, 47L, 45L, 52L, 41L, 46L, 57L)), .Names = c("id", "sex", "score"), class = "data.frame", row.names = c(NA, -9L)) </code></pre> <p>I want to write some if statements based on score for males and females. The basic if function would go like this:</p> <pre><code>df$score3&lt;-ifelse(df$score &lt;45,"low", ifelse(df$score&gt;=45 &amp; df$score&lt;55,"normal", ifelse(df$score &gt;=55,"high", NA))) </code></pre> <p>How would I change this expression for males only (separate cut offs will be used for females (say low = &lt;50, normal = >=50 &amp; &lt;58, high = >=58)).</p> <p>If any advice could be given on using if statements based on another column within a dataframe, I would be most grateful.</p>