Home > OS >  subset based on two condition from grouped dataframe
subset based on two condition from grouped dataframe

Time:12-10

In my data, I'm trying to subset unique study values where reporting is both subscale and composite.

In this data, the desired output will be the rows of study==1. I have tried the following with no success. Is there any fix?

library(tidyverse)
m="
study  reporting
1      subscale
1      composite
2      subscale
2      subscale
3      composite"

data <- read.table(text = m, h=T)

data %>% group_by(study) %>% 
  filter(reporting=="composite"&reporting=="subscale") # Doesn't return anything

CodePudding user response:

You may try -

library(dplyr)

data %>% 
  group_by(study) %>% 
  filter(all(c('composite', 'subscale') %in% reporting)) %>%
  ungroup()

#  study reporting
#  <int> <chr>    
#1     1 subscale 
#2     1 composite
  • Related