Assign NAs to Duplicates in each Row after first occurence

一曲冷凌霜 提交于 2019-12-08 05:08:15

问题


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

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

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

Any ideas on this?

Thank you!

Dataframe: http://pastebin.com/yKqhWyvW


回答1:


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)))


来源:https://stackoverflow.com/questions/42142260/assign-nas-to-duplicates-in-each-row-after-first-occurence

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!