首次出现后将 NA 分配给每行中的重复项 [英] Assign NAs to Duplicates in each Row after first occurence

查看:9
本文介绍了首次出现后将 NA 分配给每行中的重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的数据框:

I have a dataframe that looks like this:

    crop1   crop4       crop5       crop6   crop7   crop8         crop9  crop10      crop11
1   Onion    Rice  Soya Beans  Sugar Cane   Onion     Tea  Corn (Maize)    Rice  Soya Beans
2    None    None        None        None    None    None          None    None        None
3    None    None        None        None    None    None          None    None        None
4 Accacia  Rubber     Accacia      Rubber Accacia  Rubber       Accacia  Rubber     Accacia
5 Accacia  Rubber     Accacia      Rubber Accacia  Rubber       Accacia  Rubber     Accacia
6    Rice    Rice        Rice        Rice    Rice    Rice          Rice    Rice        Rice

现在我想在它们第一次出现后将 NA 分配给每行中的所有重复条目.也就是说,我的数据框应该在每一行中只有唯一的条目,否则 NAs.例如,在第一行我想要:

Now i would like to assign NAs to all repeated entries in each row after they first occured. That is, my dataframe should have just unique entries in each row, otherwise NAs. For example, in the first row I would like to have:

1   Onion    Rice  Soya Beans  Sugar Cane   NA    Tea  Corn (Maize)    NA  NA

由于洋葱"、米饭"和大豆"已经出现在这一行.因此,第 4 行应如下所示:

As "Onion", "Rice" and "Soya Beans" have already occured in this row. Consequently, row 4 should look like this:

4 Accacia  Rubber NA NA NA NA NA NA NA

对此有什么想法吗?

谢谢!

数据框:http://pastebin.com/yKqhWyvW

推荐答案

我们可以使用 duplicated 通过遍历行来将每行中的重复元素设置为 NA申请

We can use duplicated to set the duplicate elements as NA in each row by looping over the rows with apply

df1[] <- t(apply(df1, 1, FUN = function(x) replace(x, duplicated(x), NA)))

这篇关于首次出现后将 NA 分配给每行中的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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