Austin
Austin

Reputation: 73

Data Replacement based on conditions in R

I am trying to find an equivalent to if, then, else in R for data replacement.

Example using iris R data set.

I want to be able to calculate and replace data with the following type of condition:

IF Sepal.Width >= 2.8 and <= 3.1 AND petal.length < 1.4 THEN petal.length = 1.

Haven't figured out how to do this in R. I know you can set conditions to specific columns, but I need it to evaluate the values of a column to determine which columns need the adjustment. I have seen several examples but none evaluate 2 columns before determining the replace.

Thank you so much for any help!

Upvotes: 1

Views: 1882

Answers (1)

Dev Patel
Dev Patel

Reputation: 292

Or you can do something like this - very similar to what Codoremifa suggested above.

    df$Petal.Length[df$Sepal.Width >= 2.8 & df$Sepal.Width <= 3.1 & df$Petal.Length < 1.4 ] = 1

Upvotes: 2

Related Questions