删除特定列中具有特定值的重复行

时间:2020-03-27 15:31:11

标签: r dataframe

我有一个数据框,我想删除除一列以外在所有列中重复的行,并选择保留不是特定值的行。

enter image description here

在上面的示例中,除了col3之外,所有列均重复了第三行和第四行,因此我只想保留一行。复杂的步骤是我要保留第4行而不是第3行,因为col3中的第3行被“排除”。通常,我只想保留没有被“排除”的行(重复的行)。

我的真实数据帧中有很多重复的行,并且在重复的那两行中,肯定有一个被“排除”。

以下是可复制的,例如:

a <- c(1,2,3,3,7)
b <- c(4,5,6,6,8)
c <- c("red","green","excluded","orange","excluded")
d <- data.frame(a,b,c)

非常感谢您!

更新:或者,当删除重复项时,仅保留第二个观察值(第4行)。

5 个答案:

答案 0 :(得分:0)

具有一些基数R的dplyr应该适用于此:

 library(dplyr) 
 a <- c(1,2,3,3,3,7)
 b <- c(4,5,6,6,6,8)
 c <- c("red","green","brown","excluded","orange","excluded")
 d <- data.frame(a,b,c)

 d <- filter(d, !duplicated(d[,1:2]) | c!="excluded")

Result: 
  a b        c
1 1 4      red
2 2 5    green
3 3 6    brown
4 3 6   orange
5 7 8 excluded

过滤器将清除所有应排除且不可重复的内容。我还向您的示例(“棕色”)添加了一个非唯一排除示例,以进行测试。

答案 1 :(得分:0)

这是一个带有循环的示例:

a <- c(1,2,3,3,7)
b <- c(4,5,6,6,8)
c <- c("red","green","excluded","orange","excluded")
d<- data.frame(a,b,c)

# Give row indices of duplicated rows (only the second and more occurence are given)
duplicated_rows=which(duplicated(d[c("a","b")]))

to_remove=c()
# Loop over different duplicated rows
for(i in duplicated_rows){
  # Find simmilar rows
  selection=which(d$a==d$a[i] & d$b==d$b[i])
  # Sotre indices of raw in the set of duplicated row whihc are "excluded"
  to_remove=c(to_remove,selection[which(d$c[selection]=="excluded")])
}

# Remove rows
d=d[-to_remove,]

print(d)

> a b       c
> 1 4      red
> 2 2 5   green
> 4 3 6   orange
> 5 7 8  excluded

答案 2 :(得分:0)

这是一种可能……希望能对您有所帮助:)

nquit <- (d %>%
  mutate(code= 1:nrow(d)) %>%
  group_by(a, b) %>%
  mutate(nDuplicate= n()) %>%
  filter(nDuplicate > 1) %>%
  filter(c == "excluded"))$code

e <- d[-nquit]

答案 3 :(得分:0)

另一种dplyr解决方案通过@Klone缩短了方法:

d %>% mutate(c = factor(c, ordered = TRUE, 
                        levels = c("red", "green", "orange", "excluded"))) %>% # Order the factor variable
  arrange(c) %>% # Sort the data frame so that excluded comes first
  group_by(a, b) %>% # Group by the two columns that determine duplicates
  mutate(id = 1:n()) %>% # Assign IDs in each group
  filter(id == 1) # Only keep one row in each group

结果:

# A tibble: 4 x 4
# Groups:   a, b [4]
      a     b c           id
  <dbl> <dbl> <ord>    <int>
1     1     4 red          1
2     2     5 green        1
3     3     6 orange       1
4     7     8 excluded     1

答案 4 :(得分:0)

关于问题末尾的修改:

更新:或者,当删除重复项时,仅保留第二个观察值(第4行)。

请注意,如果col3对行的排序确定了要保留的行始终是重复记录中的最后一个,则只需设置{{1} } fromLast=TRUE中的}来请求将行标记为重复行从每个重复组的最后一个找到的重复计数开始

使用稍有修改的数据版本(在其中添加了更多重复组,以更好地表明该过程在更一般的情况下有效)

duplicated()

使用:

a <- c(1,1,2,3,3,3,7)
b <- c(4,4,5,6,6,6,8)
c <- c("excluded", "red","green","excluded", "excluded","orange","excluded")
d <- data.frame(a,b,c)

  a b        c
1 1 4 excluded
2 1 4      red
3 2 5    green
4 3 6 excluded
5 3 6 excluded
6 3 6   orange
7 7 8 excluded

我们得到:

ind2remove = duplicated(d[,c("a", "b")], fromLast=TRUE)
(d_noduplicates = d[!ind2remove,])

请注意,这并不需要每个重复组中的行在原始数据中都在一起。唯一重要的是,您要保留每个重复组中数据中最后出现的记录