R “Error: unexpected ‘}’ in “}”

Your code has a number of issues:

  1. The if statement would need enclosing {} symbols.
  2. The if also needs enclosing () symbols around the logical expression.
  3. The result of the recode function is not stored anywhere (the function does not change values-in-place).
  4. Loops are a poor solution to this problem.

It would be much simpler to take advantage of R’s natural vectorization. Rather than an if inside a for loop, you can this all in one line:

data$terminate_reason[data$terminate_reason == '(Null Value)'] <- NA

That should do the trick, but ensure that the “terminate_reason” column is character, not factor.

Leave a Comment