2011-10-17 9 views
30

のために、私は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個の要素の名前を与えますが、私は(すべてのユニークな組み合わせで、一緒にyadper)2つの列を抽出する必要があります。

yad   per 
---   --- 
BARBIE  AYLIK 
BAKUGAN  2 AYLIK 
MICKEY MOUSE 2 AYLIK 
TINKERBELL 3 AYLIK 
...   ... 

どのように私はこれを達成することができますか?

答えて

75

unique()はどうですか?

df <- data.frame(yad = c("BARBIE", "BARBIE", "BAKUGAN", "BAKUGAN"), 
       per = c("AYLIK", "AYLIK", "2 AYLIK", "2 AYLIK"), 
       hmm = 1:4) 

df 
#  yad  per hmm 
# 1 BARBIE AYLIK 1 
# 2 BARBIE AYLIK 2 
# 3 BAKUGAN 2 AYLIK 3 
# 4 BAKUGAN 2 AYLIK 4 

unique(df[c("yad", "per")]) 
#  yad  per 
# 1 BARBIE AYLIK 
# 3 BAKUGAN 2 AYLIK 
+1

+の

ユニークな組み合わせになります1文字列の正規化(tolower、特殊文字の出力など)もお勧めします。 –

+0

'df'が行列の場合はどうすればいいですか? 'data.frame'に変換するか、それを行う関数がありますか? – sop

+2

実際に私は作品を行った 'unique.matrix()'を見つけましたが、とにかくありがとう、 – sop

5

一連の要因のすべての固有の組み合わせを取得するには、いくつかの方法があります。

with(df, interaction(yad, per, drop=TRUE)) # gives labels 
with(df, yad:per)       # ditto 

aggregate(numeric(nrow(df)), df[c("yad", "per")], length) # gives a data frame 
7

これはJoshの回答に追加されたものです。

library(data.table) 

#create data table 
dt <- data.table(
    V1=LETTERS[c(1,1,1,1,2,3,3,5,7,1)], 
    V2=LETTERS[c(2,3,4,2,1,4,4,6,7,2)], 
    V3=c(1), 
    V4=c(2)) 

> dt 
# V1 V2 V3 V4 
# A B 1 2 
# A C 1 2 
# A D 1 2 
# A B 1 2 
# B A 1 2 
# C D 1 2 
# C D 1 2 
# E F 1 2 
# G G 1 2 
# A B 1 2 

# set the key to all columns 
setkey(dt) 

# Get Unique lines in the data table 
unique(dt[list(V1, V2), nomatch = 0]) 

# V1 V2 V3 V4 
# A B 1 2 
# A C 1 2 
# A D 1 2 
# B A 1 2 
# C D 1 2 
# E F 1 2 
# G G 1 2 

警告:data.table

例で重複行を除外しながら、

また、他の変数の値を保つことができる他の変数の値の異なる組み合わせがある場合、あなたの結果は、V1とV2

+0

奇妙な、ユニークな操作は動作しますが、結果dtはNAに設定された他のすべての列を持ちます。なぜなのかご存知ですか? –

+0

ありがとうございました。この操作によってマージが行われ、いくつかの「NA」値が生成されます。解決策は 'allow.cartesian = TRUE'を' nomatch = 0'に置き換えることです。結果には 'NA'値は無視されます。私は答えを更新しました。ありがとう –

-1
df$new_var = paste(df$yad,df$per,sep = "_") 
length(unique(df$new_var)) #for checking 
df = df[!duplicated(df$new_var),] 
nrow(df) # for checking , this should be equal to 2nd line output 
df$new_var = NULL 
+0

これは単なる値を与えるだけではなく、オリジナルのdata.frameを上書きします。 OPが求めているものではありません。 – BenBarnes

+0

上書きしたくない場合は、簡単です。最初の3行目にdfの代わりにdf2を入れてください.DONE – ashok

関連する問題