R语言过滤数据框
filter a data frame in R language
我是 R 的新手,我需要从纳斯达克找到那些包含“I”和“S”字母的代码。
我写了一个正则表达式:
(?i)\b(?=\p{L}*I)(?=\p{L}*S)\p{L}+\b
但是当我尝试过滤时遇到了麻烦:
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))
我做错了什么?
这个输出符合你的要求吗?
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>
由 reprex package (v2.0.1)
创建于 2022-05-31
我是 R 的新手,我需要从纳斯达克找到那些包含“I”和“S”字母的代码。
我写了一个正则表达式:
(?i)\b(?=\p{L}*I)(?=\p{L}*S)\p{L}+\b
但是当我尝试过滤时遇到了麻烦:
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))
我做错了什么?
这个输出符合你的要求吗?
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>
由 reprex package (v2.0.1)
创建于 2022-05-31