-4

Curious if it is possible to input a list of company names like c("Netflix", "Tesla", "Apple") and it returns the associated Ticker names such as c("NFLX", "TSLA", "AAPL") which I can then input into a function I created which returns the current closing prices. I simply want to automate the manual lookup of each company on Yahoo Finance. Please let me know if anyone has any suggestions! Thanks

Mossa
  • 1,656
  • 12
  • 16
sdda
  • 1
  • 1

1 Answers1

1

1) Those are all S&P 500 companies so if that is sufficient then get the list from Wikipedia.

library(rvest)
library(dplyr)

stocks <- data.frame(Symbol = c("TSLA", "NFLX", "AAPL"))

u <- "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
html <- read_html(u)
tab <- html_table(html)[[1]]

stocks %>%
  left_join(tab, by = "Symbol") %>%
  select(Symbol, Security)

giving:

  Symbol Security
1   TSLA    Tesla
2   NFLX  Netflix
3   AAPL    Apple

2) Even easier is to use the csv file at the url below giving the same result except for the second column name which is called Name rather than Security.

library(dplyr)

stocks <- data.frame(Symbol = c("TSLA", "NFLX", "AAPL"))

u2 <- "https://raw.githubusercontent.com/datasets/s-and-p-500-companies/master/data/constituents.csv"
tab2 <- read.csv(u2)
stocks %>%
  left_join(tab2, by = "Symbol") %>%
  select(Symbol, Name)
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341