通过将数字列与固定值进行比较来创建类别 [英] Create categories by comparing a numeric column with a fixed value

查看:74
本文介绍了通过将数字列与固定值进行比较来创建类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑 iris 数据:

 iris 
        Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
    1            5.1         3.5          1.4         0.2     setosa
    2            4.9         3.0          1.4         0.2     setosa
    3            4.7         3.2          1.3         0.2     setosa
    4            4.6         3.1          1.5         0.2     setosa
    5            5.0         3.6          1.4         0.2     setosa
    6            5.4         3.9          1.7         0.4     setosa
    7            4.6         3.4          1.4         0.3     setosa

我想基于变量 Sepal.Length 中具有固定限制/截止值的值的比较来创建新列,例如检查值是否大于或小于5:

I want to create a new column based on a comparison of the values in variable Sepal.Length with a fixed limit / cut-off, e.g. check if the values are larger or smaller than 5:

如果Sepal.Length> = 5,则分配 UP。否则将 DOWN 分配给新列 Regulation。

if Sepal.Length >= 5 assign "UP" else assign "DOWN" to a new column "Regulation".

这样做的方式是什么?

推荐答案

尝试

iris$Regulation <- ifelse(iris$Sepal.Length >=5, "UP", "DOWN")

这篇关于通过将数字列与固定值进行比较来创建类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆