Home > OS >  R dataframe column filter by the beginning of its characters
R dataframe column filter by the beginning of its characters

Time:09-07

I want to visualize all products that have a specific classification_id beginning with 8 (in order to explore all of its subcodes classification by displaying also the classification_name). Below you have the table used as dataframe:

enter image description here

What I tried to do it with a pipe operator are the following codes, all of them with an error telling me that the pipe operator is not defined as a function:

  1. Option 1: sales_pharmacy %>% filter(classification_id == 8*) %>% select(classification_id, classification_name)

  2. Option 2: sales_pharmacy %>% filter(grepl(pattern = 8*, x = classification_id) %>% select(classification_id, classification_name)

The error in the second case is: Error in sales_pharmacy %>% filter(grepl(pattern = 8, x = classification_id)) : could not find function "%>%"

I have searched in many ways, but I can't find the solution anywhere for such a simple problem. I have been told that the symbol "*" can serve for searching all that can be left in every number or string, but it doesn't seem to work in this case where I search for everything left that starts with an 8 in column classification_id . Any suggestions?

CodePudding user response:

Did you load the required packages to use the pipe operator?

I would do:

library(tidyverse)
sales_pharmacy %>%
  mutate(filter_id = as.character(classification_id)) %>%
  filter(str_detect(filter_id, '$8'))

What we are doing here is to first make sure that we have a character representstion of your id, and then we filter on those that start with an 8 using regex.

CodePudding user response:

How about sales_pharmacy %>% mutate (classification_select = substr(as.character(classification_id),1,1) %>% filter(classification_select == "8")

  • Related