刪除重復(fù)行我讀過CSV文件放入R數(shù)據(jù)幀中。一些行在其中一個列中具有相同的元素。我想刪除該列中重復(fù)的行。例如:platform_external_dbus 202 16 google 1platform_external_dbus 202 16 space-ghost.verbum 1platform_external_dbus 202 16 localhost 1platform_external_dbus 202 16 users.sourceforge 8platform_external_dbus 202 16 hughsie 1我只想要這些行中的一行,因為其他行在第一列中有相同的數(shù)據(jù)。
3 回答

冉冉說
TA貢獻1877條經(jīng)驗 獲得超1個贊
# in the above example, you only need the first three columnsdeduped.data <- unique( yourdata[ , 1:3 ] ) # the fourth column no longer 'distinguishes' them, # so they're duplicates and thrown out.

慕斯王
TA貢獻1864條經(jīng)驗 獲得超2個贊
對于來此尋找重復(fù)行刪除的一般答案的人,請使用!duplicated():
a <- c(rep("A", 3), rep("B", 3), rep("C",2))
b <- c(1,1,2,4,1,1,2,2)
df <-data.frame(a,b)
duplicated(df)
[1] FALSE TRUE FALSE FALSE FALSE TRUE FALSE TRUE
> df[duplicated(df), ]
a b
2 A 1
6 B 1
8 C 2
> df[!duplicated(df), ]
a b
1 A 1
3 A 2
4 B 4
5 B 1
7 C 2

牛魔王的故事
TA貢獻1830條經(jīng)驗 獲得超3個贊
distinct()
數(shù)據(jù):
dat <- data.frame(a = rep(c(1,2),4), b = rep(LETTERS[1:4],2))
刪除指定列重復(fù)的行:
library(dplyr)dat %>% distinct(a, .keep_all = TRUE) a b1 1 A2 2 B
刪除與其他行完全重復(fù)的行:
dat %>% distinct a b1 1 A2 2 B3 1 C4 2 D
- 3 回答
- 0 關(guān)注
- 750 瀏覽
添加回答
舉報
0/150
提交
取消