0

I'm fairly new to R and was hoping to get some help on an issue I'm having.

I want to for loop through an input column with numeric values, and depending on whether those values are greater/less than a given value, I want it to output "above cutoff", "below cutoff" or "borderline". I specifically want the output to be in a new column, outputColumn, and in the corresponding row of the input value.

Here is what I have tried so far:

for (i in inputColumn) {  
  if (i <= 5)
    data$outputColumn <- "BelowCutoff"
  if (i >= 20)
    data$outputColumn <- "AboveCutOff"
  else
    data$outputColumn <- "Borderline"
}

When I execute this for loop, a new column is made called "outputColumn", but every single row in the column says "borderline", which isn't correct. My inputColumn has rows with values above 20 and values 5, so I am expecting to get some rows that say "belowcutoff" or "abovecutoff".

I appreciate any input and help!

LiYe523
  • 1
  • 1
  • 1
    In the future it's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Nov 29 '21 at 06:48

0 Answers0