分享
三行代码  ›  专栏  ›  技术社区  ›  scamander

如何用dplyr检查数据帧列表列中是否包含向量

  •  0
  • scamander  · 技术社区  · 6 天前

    library(tidyverse)
    
    dat <- tribble(~cell, ~status, 
            "A", "x+", 
            "A", "y-",
            "A", "z+", 
            "B", "x-",
            "B", "y-", 
            "B", "z+")
    

    然后我根据 cell 并构造列表列。 是否包含它。

    wanted_status <- c("x+", "y-")
    dat %>% 
      group_by(cell) %>% 
      mutate(nstatus = list(status)) %>% # construct list column
      dplyr::select(-status) %>% 
      unique() %>% 
      mutate(contained = if_else(wanted_status %in% nstatus, "in", "out")) # check if wanted_status vector is contained in nstatus or not.
    

    通过这个例子,我希望结果是:

      cell  contained  
      A     in
      B     out
    

    我怎样才能做到这一点?

    Error: Problem with `mutate()` input `contained`.
    x Input `contained` can't be recycled to size 1.
    ℹ Input `contained` is `if_else(wanted_status %in% nstatus, "in", "out")`.
    ℹ Input `contained` must be size 1, not 2.
    ℹ The error occurred in group 1: cell = "A".
    
    1 回复  |  直到 6 天前
        1
  •  2
  •   Ronak Shah    6 天前

    你想检查一下吗 价值 wanted_status

    library(dplyr)
    
    wanted_status <- c("x+", "y-")
    
    dat %>%
      group_by(cell) %>%
      summarise(contained = if(all(wanted_status %in% status)) 'in' else 'out')
    
    #  cell  contained
    #  <chr> <chr>    
    #1 A     in       
    #2 B     out