基于R中条件的数据替换

基于R中条件的数据替换,r,replace,conditional-statements,R,Replace,Conditional Statements,我试图在R中找到一个等价于if,then,else的数据替换 使用iris R数据集的示例 我希望能够计算并用以下类型的条件替换数据: IF Sepal.Width >= 2.8 and <= 3.1 AND petal.length < 1.4 THEN petal.length = 1. 如果萼片宽度>=2.8并且或者你可以这样做-非常类似于Codoremifa上面所建议的 df$Petal.Length[df$Sepal.Width >= 2.8 &

我试图在R中找到一个等价于if,then,else的数据替换

使用iris R数据集的示例

我希望能够计算并用以下类型的条件替换数据:

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

如果萼片宽度>=2.8并且或者你可以这样做-非常类似于Codoremifa上面所建议的

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

df$Petal.Length[df$Sepal.Width>=2.8和df$Sepal.Width
df[df$Sepal.Width>=2.8和df$Sepal.Width@Codoremifa:为什么不将您的评论升级为真正的答案?