2017-02-09 52 views
1

后,我有一个数据帧,看起来像这样:分配港定居人士重复每行第一次出现

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。例如,在第一行中我想有:

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

为“洋葱”,“米”和“播大豆”在此行中已经发生。因此,第4行应该看起来像这样:

4 Accacia Rubber NA NA NA NA NA NA NA 

对此有何看法?

谢谢!

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

回答

1

我们可以使用duplicated通过遍历行与apply

df1[] <- t(apply(df1, 1, FUN = function(x) replace(x, duplicated(x), NA))) 
设置各行中的重复元素作为 NA
相关问题