Apply if else condition to make new column in r

Viewed 1198

I'd like to use an if else statement to make a new column in my dataframe based on data in another column. I've looked at a number of prior (such as this one and this one), but seem to be doing something wrong as I either get an error or no new column.

I've tried making an ifelse function:

  if(x >= 4000)
{print (">4000")
  } else if (x >=3000 & x <= 4000) 
    {print ("3000-4000")
    } else if  (x >=2000 & x <= 3000) 
    {print("2000-3000")
      } else if (x >=1000 & x <= 2000)
      {print("1000-2000")
      } else print ("<1000")}

this function works/ran but I can't figure out how to apply it to one column in my dataframe (I've tried this dat$P.bins <- Bins(dat$Pcol) but get the following error: the condition has length > 1 and only the first element will be used1 ">4000"

I've also tried to run a ifelse statement:

dat$P.bin<- ifelse(P.col>=4000, ">4000",
                                ifelse(P.col <=4000 & >= 3000, "3000-4000"),
                                ifelse(P.col<=3000 & >= 2000, "2000-3000"), 
                                ifelse(P.col <=2000 & >=1000, "1000-2000"), 
                                ifelse(P.col <1000, "1000"))

but get this error: Error: unexpected '>=' in:"dat$P.bins <- ifelse(Pcol >=4000, ">4000",felse(Pcol <=4000 & >=". With this statement I'm not sure how to do a range in the ifelse statement.

Any help or guidance would be greatly appreciated!

2 Answers

We can use case_when like this:

library(tidyverse)

dat <- tibble(P.col = seq(0, 20000, 1000))

mutate(dat, P.bin = case_when(P.col >= 4000 ~ ">4000",
                              P.col <= 3000 & P.col >= 2000 ~ "2000-3000",
                              P.col <= 3000 & P.col >= 2000 ~ "2000-3000",
                              P.col <= 2000 & P.col >=1000 ~ "1000-2000",
                              P.col < 1000 ~ "1000"))
#> # A tibble: 21 x 2
#>    P.col P.bin    
#>    <dbl> <chr>    
#>  1     0 1000     
#>  2  1000 1000-2000
#>  3  2000 2000-3000
#>  4  3000 2000-3000
#>  5  4000 >4000    
#>  6  5000 >4000    
#>  7  6000 >4000    
#>  8  7000 >4000    
#>  9  8000 >4000    
#> 10  9000 >4000    
#> # … with 11 more rows

Created on 2021-06-11 by the reprex package (v2.0.0)

The ifelse approach you are using is correct but you have some syntax issues.

  • You are not closing the brackets at right place.
  • No mention of dataframe name in ifelse. P.col in itself isn't enough.
  • P.col <=4000 & >= 3000 is not valid. You need P.col <=4000 & P.col >= 3000.

Try the following code -

dat$P.bin<- with(dat, ifelse(P.col>=4000, ">4000",
                   ifelse(P.col <=4000 & P.col >= 3000, "3000-4000",
                   ifelse(P.col<=3000 & P.col >= 2000, "2000-3000", 
                   ifelse(P.col <=2000 & P.col >=1000, "1000-2000", 
                   ifelse(P.col <1000, "1000", NA_character_))))))

Having said that using case_when as suggested by @jpdugo17 might be cleaner way to do this.

Related