Home > Software engineering >  filter a data frame in R language
filter a data frame in R language

Time:05-31

I am new to R and I need to find those tickers from NASDAQ which contain both "I" and "S" letters in it.

I wrote a regexp:

(?i)\b(?=\p{L}*I)(?=\p{L}*S)\p{L} \b

But troubles are met when I try to filter :

library(quantmod)
library(dplyr)
library(stringr)
library(tidyverse)

data <- stockSymbols(exchange="NASDAQ")
values <- as.data.frame(data$NASDAQ.Symbol)
filter(values, values==grep("/(?i)\b(?=\p{L}*I)(?=\p{L}*S)\p{L} \b/"), values))

What am I doing wrong?

CodePudding user response:

Does this output meet your requirements?

library(quantmod)
#> Loading required package: xts
#> Loading required package: zoo
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
#> Loading required package: TTR
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
library(tidyverse)

data <- stockSymbols(exchange = "NASDAQ")
#> Fetching NASDAQ symbols...

tibble(data) %>%
  filter(str_detect(NASDAQ.Symbol, "(I|i) "),
         str_detect(NASDAQ.Symbol, "(S|s) ")) %>% 
  relocate(NASDAQ.Symbol)
#> # A tibble: 172 × 17
#>    NASDAQ.Symbol Symbol Name          LastSale MarketCap IPOyear Sector Industry
#>    <chr>         <chr>  <chr>         <lgl>    <lgl>     <lgl>   <lgl>  <lgl>   
#>  1 ABSI          ABSI   Absci Corpor… NA       NA        NA      NA     NA      
#>  2 AEIS          AEIS   Advanced Ene… NA       NA        NA      NA     NA      
#>  3 AIHS          AIHS   Senmiao Tech… NA       NA        NA      NA     NA      
#>  4 AIRS          AIRS   AirSculpt Te… NA       NA        NA      NA     NA      
#>  5 BBSI          BBSI   Barrett Busi… NA       NA        NA      NA     NA      
#>  6 BIOS          BIOS   BioPlus Acqu… NA       NA        NA      NA     NA      
#>  7 BIOSU         BIOSU  BioPlus Acqu… NA       NA        NA      NA     NA      
#>  8 BIOSW         BIOSW  BioPlus Acqu… NA       NA        NA      NA     NA      
#>  9 BIS           BIS    ProShares Ul… NA       NA        NA      NA     NA      
#> 10 BITS          BITS   Global X Blo… NA       NA        NA      NA     NA      
#> # … with 162 more rows, and 9 more variables: Exchange <chr>, Test.Issue <lgl>,
#> #   Round.Lot.Size <int>, ETF <lgl>, Market.Category <chr>,
#> #   Financial.Status <chr>, Next.Shares <lgl>, ACT.Symbol <lgl>,
#> #   CQS.Symbol <lgl>

Created on 2022-05-31 by the reprex package (v2.0.1)

  • Related