0

When i'm trying to run an if and else if statements in R

enter image description here

I'm getting these warnings, so shouldn't we use if statements on dataframes and their columns at all? are they used only for single vectors?

enter image description here

Can someone please help

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • 3
    Please don't share data/code as images, share them as text instead. Yes `if`/`else` are used only for scalars for vectors use `ifelse`. – Ronak Shah Jul 10 '20 at 06:26

1 Answers1

2

As mentioned in the previous comment, ifelse works with vectors.

This should work:

logik <- mtcars

ifelse(logik$mpg < 10, print("less than 10"), 
     ifelse(logik$mpg > 10 & logik$mpg < 20, "Between 10 and 20", "Greater than 20"))
Edward
  • 10,360
  • 2
  • 11
  • 26
vlad_a
  • 21
  • 1