刪除重復行我讀過CSV文件放入R數據幀中。一些行在其中一個列中具有相同的元素。我想刪除該列中重復的行。例如: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我只想要這些行中的一行,因為其他行在第一列中有相同的數據。
3 回答

冉冉說
TA貢獻1877條經驗 獲得超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條經驗 獲得超2個贊
對于來此尋找重復行刪除的一般答案的人,請使用!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
- 3 回答
- 0 關注
- 756 瀏覽
添加回答
舉報
0/150
提交
取消