在R中使用二进制数据或存在/不存在数据创建新列 [英] Create new column with binary data or presence/absence data in R

查看:57
本文介绍了在R中使用二进制数据或存在/不存在数据创建新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下问题的答案比该答案更简单:

The answer to the following question has been addressed more simply than this answer: Create new column with binary data based on several columns

我正在尝试基于 R 中的另一列创建一个新的二进制数据列(存在/不存在数据).

I am trying to create a new column of binary data (presence/absence data) based on another column in R.

我希望编号为101的"Species_code"行在新的"Presence_absence"列中产生1;其他所有东西都应该产生0.

I want "Species_code" rows with the number 101 to produce a 1 in the new "Presence_absence" column; everything else should produce a 0.

这就是我想要的新的Presence_absence列的样子:

Here is what I want the new Presence_absence column to look like:

Species_code       Presence_absence
101                1
103                0
101                1
99                 0
101                1

推荐答案

使用 ifelse :

> df <- data.frame(Species_code = c(101, 103,101,99,101)) # your data
> df$Presence_absence <- ifelse(df$Species_code==101, 1, 0) # this does the trick
> df
  Species_code Presence_absence
1          101                1
2          103                0
3          101                1
4           99                0
5          101                1

这篇关于在R中使用二进制数据或存在/不存在数据创建新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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