8

I'd like to learn how to conditionally replace values in R data frame using if/then statements. Suppose I have a data frame like this one:

df <- data.frame(
customer_id = c(568468,568468,568468,485342,847295,847295),
customer = c('paramount','paramount','paramount','miramax','pixar','pixar'));

I'd like to do something along the lines of, "if customer in ('paramount','pixar') make customer_id 99. Else do nothing". I'm using this code, but it's not working:

if(df$customer %in% c('paramount','pixar')){
df$customer_id == 99
}else{
df$customer_id == df$customer_id
}

I get a warning message such as the condition has length > 1 and only the first element will be used. And the values aren't replaced.

I'd also like to know how to do this using logical operators to perform something like, "if customer_id >= 500000, replace customer with 'fox'. Else, do nothing.

Very easy to do in SQL, but can't seem to figure it out in R.

My sense is that I'm missing a bracket somewhere?

How do I conditionally replace values in R data frame using if/then statements?

Saul Feliz
  • 668
  • 3
  • 11
  • 20

2 Answers2

12

You can use ifelse, like this:

df$customer_id <- ifelse(df$customer %in% c('paramount', 'pixar'), 99, df$customer_id)

The syntax is simple:

ifelse(condition, result if TRUE, result if FALSE)

This is vectorized, so you can use it on a dataframe column.

LAP
  • 6,605
  • 2
  • 15
  • 28
3

You are using == instead of =(Assignment Operator) in if block. And I dont think there's need of else block in your example as you are not going to change values

 if(df$customer %in% c('paramount','pixar')){
  df$customer_id = 99
 }

Above code will do the job for you

Sociopath
  • 13,068
  • 19
  • 47
  • 75
  • What if the elements I want to see if they're in the column are elements in another column? I get the warning message the condition has length > 1 and only the first element will be used. The syntax I'm using is if(df1$column1 %in% df2$column1) { df$column2 = 0 } – Saul Feliz Jan 15 '18 at 11:49
  • You want to perform above operation row wise or in general for column1 and column2? – Sociopath Jan 15 '18 at 12:25
  • Yes, I think you're right. How do I conditionally test, and apply an operation row wise? – Saul Feliz Jan 15 '18 at 19:18
  • You can use **apply** for row wise operations. – Sociopath Jan 16 '18 at 05:41