R:在数据框的列中提取唯一值

Cyr*_*oel 7 r unique dataframe

我在R中有一个数据框,我想知道是否有可能检索其他列中不存在的列的值以及每列的这个值.

My dataframe looks like :

sample_1 sample_2 sample_3
   a        a        a       
   c        e        c
   d        f        e
   g        m        j
   m        n        n
   x        u        w
   t        z        z
Run Code Online (Sandbox Code Playgroud)

我想得到以下结果:

sample_1 sample_2 sample_3
   d        f        j
   g        u        w
   x
   t
Run Code Online (Sandbox Code Playgroud)

提前感谢您的回答,

akr*_*run 5

你可以试试

lst <- lapply(seq_along(df1), function(i) df1[,i][!df1[,i] %in%
                      unique(unlist(df1[-i]))])
 library(stringi)
 as.data.frame(stri_list2matrix(lst, fill=''))
Run Code Online (Sandbox Code Playgroud)

  • 或类似地:`lapply(1:ncol(df),function(i)setdiff(df [,i],unlist(df [-i]))) (5认同)