我在R中有以下数据框:
> str(df)
'data.frame': 545227 obs. of 15 variables:
$ ykod : int 93 93 93 93 93 93 93 93 93 93 ...
$ yad : Factor w/ 42 levels "BAKUGAN","BARBIE",..: 30 30 30 30 30 30 30 30 30 30 ...
$ per : Factor w/ 3 levels "2 AYLIK","3 AYLIK",..: 3 3 3 3 3 3 3 3 3 3 ...
$ donem: int 201101 201101 201101 201101 201101 201101 201101 201101 201101 201101 ...
$ sayi : int 201101 201101 201101 201101 201101 201101 201101 201101 201101 201101 ...
$ mkod : int 4 5 9 11 12 18 20 22 25 26 ...
$ mad : Factor w/ 10464 levels " Defne Market ",..: 405 8075 9710 10145 9297 7973 2542 3892 2759 5769 ...
$ mtip : Factor w/ 29 levels "Abone Bürosu ",..: 2 20 20 2 2 2 2 2 2 2 ...
$ kanal: Factor w/ 2 levels "OB","SS": 2 2 2 2 2 2 2 2 2 2 ...
$ bkod : int 110565 110565 110565 110565 110565 110565 110565 110565 110565 110565 ...
$ bad : Factor w/ 212 levels "4. Levent","500 Evler",..: 167 167 167 167 167 167 167 167 167 167 ...
$ bolge: Factor w/ 12 levels "Adana Şehiriçi",..: 7 7 7 7 7 7 7 7 7 7 ...
$ sevk : int 2 3 3 3 2 2 2 6 2 2 ...
$ iade : int 2 1 0 2 0 2 1 0 0 2 ...
$ satis: int 0 2 3 1 2 0 1 6 2 0 ...
我想要列出多个变量的唯一值(类似于SQL中的DISTINCT)。例如,unique(yad)
可以给我每个42个元素的名称,但我需要提取两列(yad
和 per
),并列出所有唯一的组合:
yad per
--- ---
BARBIE AYLIK
BAKUGAN 2 AYLIK
MICKEY MOUSE 2 AYLIK
TINKERBELL 3 AYLIK
... ...
我该如何实现这个目标?
df
是一个矩阵,该怎么做呢?我需要将其转换为data.frame
吗,还是有相应的函数可以实现呢? - sopunique.matrix()
函数,它已经完成了工作,不过还是谢谢你。 - sopdplyr::distinct()
是什么,但如果你想保留包含某个组合的第一个出现的整行数据,可以看一下duplicated()
函数。在这里,你可以这样做:df[!duplicated(df[1:2]),]
。 - Josh O'Briendf$dupe <- duplicated(df[c("X", "Y") ])
和df<- subset(df, dupe == FALSE)
- Bob