【问题标题】:Extract sequence of rows in R提取R中的行序列
【发布时间】:2022-07-25 18:09:02
【问题描述】:

我有这种类型的数据:

df <- structure(list(Utterance = c("(5.127)", ">like I don't understand< sorry like how old's your mom¿", 
                                   "(0.855)", "eh six:ty:::-one=", "(0.101)", "(0.487)", "[((v: gasps)) she said] ~no you're [not?]~", 
                                   "[((v: gasps)) she said] ~no you're [not?]~", "~<[NO YOU'RE] NOT (.) you can't go !in!>~", 
                                   "(0.260)", "show her [your boobs] next time"), 
                     Q = c(NA, "q_wh", "", "", NA, NA, "q_really", "", "", NA, NA), 
                     Sequ = c(NA, 1L, 1L, 1L, NA, NA, 0L, 0L, 0L, NA, NA)), class = "data.frame", row.names = c(NA, -11L))

我想提取/过滤

  • Sequ 的那些行不是 NA
  • 前一行(SequNA

到目前为止,我的尝试是定义一个获取相关行索引的函数:

QA_sequ <- function(value) {
  inds <- which(!is.na(value) & lag(is.na(value)))  
  sort(unique(c(inds-1, inds)))
}

然后通过索引切出行:

library(dplyr)
df %>% 
  slice(QA_sequ(Sequ))
                                                 Utterance        Q Sequ
1                                                  (5.127)     <NA>   NA
2 >like I don't understand< sorry like how old's your mom¿     q_wh    1
3                                                  (0.487)     <NA>   NA
4               [((v: gasps)) she said] ~no you're [not?]~ q_really    0

但是,只有前一行和第一行 Sequ 会被过滤。 我想要得到的结果是这样的

                                                  Utterance        Q Sequ
1                                                   (5.127)     <NA>   NA
2  >like I don't understand< sorry like how old's your mom¿     q_wh    1
3                                                   (0.855)             1
4                                         eh six:ty:::-one=             1
5                                                   (0.487)     <NA>   NA
6                [((v: gasps)) she said] ~no you're [not?]~ q_really    0
7                [((v: gasps)) she said] ~no you're [not?]~             0
8                 ~<[NO YOU'RE] NOT (.) you can't go !in!>~             0

编辑

我想出的解决方案感觉很麻烦:

QA_sequ <- function(value) {
  inds <- which(!is.na(value) & lag(is.na(value)))  
  sort(unique(c(inds-1)))    # extract only preceding row!
}

library(dplyr)
df %>% 
  mutate(id = row_number()) %>%
  slice(QA_sequ(Sequ)) %>%
  bind_rows(., df %>% mutate(id = row_number()) %>% filter(!is.na(Sequ))) %>%
  arrange(id)

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    这个怎么样?

    df %>%
      filter(!is.na(Sequ) | lead(!is.na(Sequ), default=FALSE))
    #                                                  Utterance        Q Sequ
    # 1                                                  (5.127)     <NA>   NA
    # 2 >like I don't understand< sorry like how old's your mom¿     q_wh    1
    # 3                                                  (0.855)             1
    # 4                                        eh six:ty:::-one=             1
    # 5                                                  (0.487)     <NA>   NA
    # 6               [((v: gasps)) she said] ~no you're [not?]~ q_really    0
    # 7               [((v: gasps)) she said] ~no you're [not?]~             0
    # 8                ~<[NO YOU'RE] NOT (.) you can't go !in!>~             0
    

    逻辑过滤器(提取)两者:

    • 所有非NA
    • 任何NA 值,其中下一个值不是NA

    【讨论】:

    • 这很好。比 OP 的方法或我的变体要简单得多。
    • default = FALSE 到底是做什么的?
    • @ChrisRuehlemann,将lead(c(T,F,T))lead(c(T,F,T), default=F)(或default=T) 进行比较。与lag(c(T,F,T))lag(c(T,F,T), default=F) 进行比较。
    【解决方案2】:

    只需添加一个额外的 OR 即可收集 sequ 不是 NA 但没有相应滞后非NA 的行...

    QA_sequ <- function(value) {
      inds <- which((!is.na(value) & lag(is.na(value))) | !is.na(value))  
      sort(unique(c(inds-1, inds)))
    }
    
    df %>%  slice(QA_sequ(Sequ))
                                                     Utterance        Q Sequ
    1                                                  (5.127)     <NA>   NA
    2 >like I don't understand< sorry like how old's your mom¿     q_wh    1
    3                                                  (0.855)             1
    4                                        eh six:ty:::-one=             1
    5                                                  (0.487)     <NA>   NA
    6               [((v: gasps)) she said] ~no you're [not?]~ q_really    0
    7               [((v: gasps)) she said] ~no you're [not?]~             0
    8                ~<[NO YOU'RE] NOT (.) you can't go !in!>~             0
    

    【讨论】:

      【解决方案3】:

      这是使用基数 R。就像您在前面的行中获取行的索引一样。

      x<-which(!is.na((df$Sequ))) 
      x1 <- x-1
      x<- unique(c(x,x1))
      x<- x[order(x)]    
      
      
      df[x,]
      

      您可以将相同的向量传递给slicedf %&gt;% slice(x)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-07-17
        • 2017-06-25
        • 2019-06-17
        • 2018-03-30
        • 2015-11-19
        • 2016-05-22
        • 1970-01-01
        相关资源
        最近更新 更多