LoginSignup
20
18

More than 5 years have passed since last update.

【R】data.frameの重複した値を持つ行を取り除く

Last updated at Posted at 2015-09-08

duplicated()とdata.frameのインデックスの操作で重複を取り除く。

# サンプルデータ
set.seed(71)
dat <- matrix(floor(runif(30, min = 0,max = 10)), ncol = 3)
dat <- as.data.frame(dat)
colnames(dat) <- c("a", "b", "c")

# a列をキーに重複した値を持つ行を除外
dat[!duplicated(dat$a), ]

# b列とc列をキーに重複した値の組を持つ行を除外
dat[!duplicated(paste(dat$b, dat$c, sep = ",")), ]

dplyrを使った場合

@yamano357さんより

# a列をキーに重複した値を持つ行を除外
dat %>% dplyr::distinct(a)

# b列とc列をキーに重複した値の組を持つ行を除外
dat %>% dplyr::distinct(b, c)

スッキリかけるみたいです。
特に制約がないのであればdplyrを使いましょう。

20
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
18