Ankush Rana
Ankush Rana

Reputation: 27

R: Filtering on the basis of row data

I have a data frame like this-

df <- structure(list(Category = c("A", "A", "A", "B", "B", "C", "C", 
"D", "D"), Subcategory = c("A_1", "A_2", "A_3", "B_1", "B_2", 
"C_1", "C_2", "D_3", "D_4"), Option = c("Q1", "Q2", "Q3", "Q4", 
"Q5", "Q6", "Q7", "Q8", "Q9")), row.names = c(NA, -9L), class = c("tbl_df", 
"tbl", "data.frame"))

Results of df :

  Category Subcategory Option
1        A         A_1     Q1
2        A         A_2     Q2
3        A         A_3     Q3
4        B         B_1     Q4
5        B         B_2     Q5
6        C         C_1     Q6
7        C         C_2     Q7
8        D         D_3     Q8
9        D         D_4     Q9

I want the output as such the data gets filtered based on Category basis . Like when I select "A", the output should be:

A_1 A_2, A_3.

and when I select A_1, I get Q1.

and subsequently for all categories.

Upvotes: 0

Views: 70

Answers (2)

TarJae
TarJae

Reputation: 78917

Use filter as needed (# optional row)

library(dplyr)
df1 <- df %>% 
  # filter(Category == "A")
  filter(Category == "A", Subcategory == "A_1")

data:

df <- tribble(
~Category, ~Subcategory, ~Option,
"A", "A_1", "Q1", 
  "A", "A_2", "Q2", 
  "A", "A_3", "Q3", 
  "B", "B_1", "Q4", 
  "B", "B_2", "Q5", 
  "C", "C_1", "Q6", 
  "C", "C_1", "Q7", 
  "D", "D_3", "Q8", 
  "D", "D_4", "Q9")

Upvotes: 0

WilliamGram
WilliamGram

Reputation: 683

Uncertain of whether I understand your application correctly, but usying dplyr the following could perhaps be what you are looking for:

library(dplyr)
df %>% 
  filter(Category == 'A') %>% 
  pull(var = SubCategory)

df %>% 
  filter(SubCategory == 'A_1') %>% 
  pull(var = Option)

I created the data below to explore.

df <- data.frame(
  Category = c(rep('A', 3), rep('B', 2), rep('C', 2), rep('D', 2)),
  stringsAsFactors = FALSE
) %>% 
  group_by(Category) %>% 
  mutate(SubCategory = paste0(Category, '_', row_number())) %>% 
  ungroup() %>% 
  mutate(Option = paste0('Q', row_number()))

Edit

You can make a function, that does what I understand you are interested in. The below is a quick and dirty approach. Probably you may find something more elegant or better tailored to your needs.

Hope it helps pointing you in the right direction.

showQuestions <- function(df, userCategory) {
  subcategories <- df %>% 
    filter(category == userCategory) %>% 
    pull(var = subCategory)
  
  print(paste0('Showing sub categories for ', userCategory, ': '))
  if (length(subcategories) == 0) {
    print('No sub categories for this category')
  } else {
    for (sc in subcategories) {
      print(paste0('Showing questions for sub category ', sc, ': '))
      questions <- df %>% 
        filter(subCategory == sc) %>% 
        pull(var = Option)
      if (length(questions) == 0) {
        print('No questions for this subcategory')
      } else {
        for (q in questions) {
          print(q)
        }
      }
    }
  }
}

# Try out e.g.
showQuestions(df, 'A')
showQuestions(df, 'E')

Upvotes: 1

Related Questions