首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何只删除连续的重复行?

如何只删除连续的重复行?
EN

Stack Overflow用户
提问于 2021-07-02 18:52:44
回答 3查看 57关注 0票数 2

我只需要删除我的数据框中的所有重复项,当它们连续出现时。我尝试过distinct()函数,但它会删除所有重复项-因此我需要一个不同的代码,以便仅当重复项连续且仅针对特定列时才有机会进行自定义和删除。

以下是我的数据示例:

代码语言:javascript
复制
 Subject  Trial Event_type  Code   Time 
    
23  VP02_RP 15  Picture face01_n    887969

24  VP02_RP 15  Sound   mpossound_test5 888260

25  VP02_RP 15  Picture pospic_test5    906623

26  VP02_RP 15  Nothing ev_mnegpos_adj_onset    928623

27  VP02_RP 15  Response    15  958962

28  VP02_RP 18  Picture face01_p    987666

29  VP02_RP 18  Sound   mpossound_test6 987668

30  VP02_RP 18  Picture negpic_test6    1006031

31  VP02_RP 18  Nothing ev_mposnegpos_adj_onset 1028031

32  VP02_RP 18  Response    15  1076642

33  VP02_RP 19  Response    13  1680887

正如您在第32和33行中看到的,我有两个连续的响应,我只想保留第一个。所以我想删除Event_type列上所有重复的连续行。

我该怎么做呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-07-02 18:56:28

您可以使用data.table中的rleid函数,它将为每个连续的事件值提供一个唯一的数字,然后使用duplicated只保留第一个值。

代码语言:javascript
复制
res <- df[!duplicated(data.table::rleid(df$Event_type)), ]
res

#   Subject Trial Event_type                    Code    Time
#23 VP02_RP    15    Picture                face01_n  887969
#24 VP02_RP    15      Sound         mpossound_test5  888260
#25 VP02_RP    15    Picture            pospic_test5  906623
#26 VP02_RP    15    Nothing    ev_mnegpos_adj_onset  928623
#27 VP02_RP    15   Response                      15  958962
#28 VP02_RP    18    Picture                face01_p  987666
#29 VP02_RP    18      Sound         mpossound_test6  987668
#30 VP02_RP    18    Picture            negpic_test6 1006031
#31 VP02_RP    18    Nothing ev_mposnegpos_adj_onset 1028031
#32 VP02_RP    18   Response                      15 1076642

base R中的rleid函数可以用rle -

代码语言:javascript
复制
res <- df[!duplicated(with(rle(df$Event_type),rep(seq_along(values), lengths))),]
res
票数 1
EN

Stack Overflow用户

发布于 2021-07-02 19:04:41

一个潜在的tidyverse解决方案:

代码语言:javascript
复制
library(tidyverse)

df1 <- data.frame(
  stringsAsFactors = FALSE,
         row.names = c("23","24","25","26","27",
                       "28","29","30","31","32","33"),
           Subject = c("VP02_RP","VP02_RP","VP02_RP",
                       "VP02_RP","VP02_RP","VP02_RP","VP02_RP","VP02_RP",
                       "VP02_RP","VP02_RP","VP02_RP"),
             Trial = c(15L, 15L, 15L, 15L, 15L, 18L, 18L, 18L, 18L, 18L, 19L),
        Event_type = c("Picture","Sound","Picture",
                       "Nothing","Response","Picture","Sound","Picture",
                       "Nothing","Response","Response"),
              Code = c("face01_n","mpossound_test5",
                       "pospic_test5","ev_mnegpos_adj_onset","15","face01_p",
                       "mpossound_test6","negpic_test6",
                       "ev_mposnegpos_adj_onset","15","13"),
              Time = c(887969L,888260L,906623L,
                       928623L,958962L,987666L,987668L,1006031L,1028031L,
                       1076642L,1680887L)
)

df1 %>%
  filter(Event_type != lag(Event_type, 1))
#>    Subject Trial Event_type                    Code    Time
#> 24 VP02_RP    15      Sound         mpossound_test5  888260
#> 25 VP02_RP    15    Picture            pospic_test5  906623
#> 26 VP02_RP    15    Nothing    ev_mnegpos_adj_onset  928623
#> 27 VP02_RP    15   Response                      15  958962
#> 28 VP02_RP    18    Picture                face01_p  987666
#> 29 VP02_RP    18      Sound         mpossound_test6  987668
#> 30 VP02_RP    18    Picture            negpic_test6 1006031
#> 31 VP02_RP    18    Nothing ev_mposnegpos_adj_onset 1028031
#> 32 VP02_RP    18   Response                      15 1076642
票数 4
EN

Stack Overflow用户

发布于 2021-07-03 00:56:17

data.table的一个选项

代码语言:javascript
复制
library(data.table)
setDT(df1)[Event_type != shift(Event_type)]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68223933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档