将低于阈值的值转换为1 [英] Convert values below a threshold into 1

查看:117
本文介绍了将低于阈值的值转换为1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下数据

x <- rnorm(100)

我想创建另一个列,其中if x 等于或大于给它一个值1或者0。

I want to create another column where where if x is equal to or greater than 0.2, give it a value of 1 or else 0.

推荐答案

我们可以创建逻辑条件并用<$ c换行$ c> +

We can create the logical condition and wrap with +

xNew <- +(x >=0.2)

如果我们需要 data.frame

dat <- data.frame(x, xNew)

或使用 ifelse

xNew <- ifelse(x >= 0.2, 1, 0) 

这篇关于将低于阈值的值转换为1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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