做一个需要抓取https://www.sec.gov/divisions/enforce/friactions/friactions2017.shtml的项目。
基本上我已经编制了SEC AAER版本列表,最终成为私人和上市公司的列表。我需要做的是从公司返回股票代码。任何R包的想法对此都有用。
作为一个例子,我希望松下公司获得“PCRFY”回报。然而,这可能是个问题:毕马威有两个上市公司,一个是“毕马威”,另一个是“毕马威公司”。如何确保两个查询都返回结果?
等式的一个例子是:
returnTicker(("Panasonic Corporation","Apple Corporation"))
哪会回来:
("PCRFY","APPL")
答案 0 :(得分:0)
希望这可以关闭到你需要的东西。它没有使用模糊匹配,但它应该具有可比较的结果。
部分改编自this question的答案。
# The TTR package includes stock symbols and names for NASDAQ, NYSE, and AMEX
library(TTR)
master <- TTR::stockSymbols()[,c('Name', 'Symbol')]
# We are going to clean up the company names by removing some unimportant words.
# Replace the words ' Incorporated', ' Corporated', and ' Corporation' with '' (no text), and put results in master$clean.
master <- cbind(master, clean = gsub(' Incorporated| Corporated| Corporation', '', master$Name))
# Some further cleaning of the master$clean column (the straight line | seperates the strings we are removing)...
master$clean <- gsub(', Inc|, Inc.| Inc| Inc.| Corp|, Corp| Corp.|, Corp.| Ltd.| Ltd', '', master$clean)
# Clean some special characters. For explanations, check out http://www.endmemo.com/program/R/gsub.php
master$clean <- gsub('\\(The\\)|[.]|\'|,', '', master$clean)
# You should also do the 3 cleaning cleaning steps above on your company names as well.
# Lastly, scroll through your data; you may find some more character strings to remove.
# Create a data frame which would contain your company names....
yourCompanyNames <- data.frame(name = c('apple', 'microsoft', 'allstate', 'ramp capital'), stringsAsFactors = F)
# This is the important part. Symbols are added to the data frame of yourCompanyNames....
yourCompanyNames$sym <- sapply(X = yourCompanyNames$name, FUN = function(YOUR.NAME) {
master[grep(pattern = YOUR.NAME, x = master$clean, ignore.case = T), 'Symbol'] })
# ------------ END ---------------
# I dunno how much R experience you have, but here is a quick explanation of what is happening, chunk-by-chunk...
# companyNames$sym <-
# Create a new column in your dataframe for the symbols we will be finding
# sapply(X = yourCompanyNames$name, FUN = function(YOUR.NAME) {
# sapply() applies a function (found on the next line) to your data (X).
# master[grep(
# grep() searches for a string in a vector of strings, and will return the indices where it is found. For example...
# grep('hel', c('hello', 'world', 'help')) returns 1 and 3
# pattern = YOUR.NAME, x = master$clean, ignore.case = T),
# The pattern which grep() is looking for is YOUR.NAME, which is an individual company name from yourCompanyNames.
# (Remember, we are moving through yourCompanyNames one-by-one)
# grep() looks for YOUR.NAME in each of the strings in master$clean, and ignores capitalization of the strings.
# 'Symbol'] })
# We can simplify the second line to master[grep(), 'Symbol']
# Since grep() is returning indicies where YOUR.NAME is found in master$clean,
# the second line gives us the symbols for the companies located at those indicies (rows).
# Finally, sapply() returns the list of symbols we found, and the list is added to yourCompanyName$sym
# Using the 4 example companies from above, we get....
# name sym
# 1 apple AAPL, APLE, DPS, MLP
# 2 microsoft MSFT
# 3 allstate ALL, ALL-PA, ALL-PB, ALL-PC, ALL-PD, ALL-PE, ALL-PF, ALL-PG
# 4 ramp capital
# The word 'apple' appeared in multiple names, and 'allstate' has multiple tickers.
# You may need to clean some of them up using fix(yourCompanyNames)
希望这会有所帮助,或者至少让你走上正确的道路。