Explore Courses Blog Tutorials Interview Questions
0 votes
in R Programming by (50.2k points)

I want to get all stock tickers/abbreviations from a page. But when I perform the web scrape the page, some unwanted tickers came with it, basically they all end with F or (space) for example "BDLL4F " or "QCOM34F". I succeeded to remove them using gsub() and regex.

stocktickers = c("PETR4", "VALE3", "MDNE3", "BDLL4F ", "QCOM34F", "SANB11", "USIM5")

stocktickers = gsub("(.*[ F]$)","NULL",stocktickers)

stocktickers = stocktickers[stocktickers!="NULL"] 

> stocktickers

[1] "PETR4"  "VALE3"  "MDNE3"  "SANB11" "USIM5" 

Is there any function that would possibly drop the string from the array if the value begins or ends with a specified character or integer?

1 Answer

0 votes
by (108k points)

For that, just use regex for removing any unwanted tickers.

For example, with grep :

stocktickers = c("PETR4", "VALE3", "MDNE3","BDLL4F ","QCOM34F", "SANB11","USIM5")

grep("(F|\\s)$",stocktickers, value = TRUE, invert = TRUE)

#[1] "PETR4"  "VALE3"  "MDNE3"  "SANB11" "USIM5" 

The above syntax will remove the values(ends either with "F" or white-space) from stocktickers.

If you want to know more about R then do refer to the R programming course.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories