返回0到第二个重复项 [英] Return 0 to second instance of duplicate

查看:92
本文介绍了返回0到第二个重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有与以下相似的数据集:

I have a similar data set to the following:

A  B   C 
1  10  5 
1  20  1
2  30  1
2  30  1

我想添加一个返回1的列,直到我们找到A&的重复项为止. B,当我需要返回0,但仅用于第二个实例时,所以:

I'd like to add a column returning 1, until we hit a duplicate of A & B, when I need to return a 0, but only for the second instance, so:

A  B   C  D
1  10  5  1
1  20  1  1
2  30  1  1
2  30  1  0

任何帮助表示赞赏.

推荐答案

一个选项是

df$D <- as.integer(!duplicated(df[c("A", "B")]))
df$D
#[1] 1 1 1 0
 

这篇关于返回0到第二个重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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