2017-06-29 52 views
0

我想在R中進行FOR語句分析股票數據。如何使用R獲取所有S&P500 corp代碼列表?

但我不知道如何獲得S & P corp代碼列表。

請讓我知道解決方案。

謝謝。

library(rpart) 
library(rpart.plot) 
library("quantmod") 
startDate = as.Date("2012-01-01") 
endDate = as.Date("2014-01-01") 
getSymbols("005930", src = "google", from = startDate, to = endDate) 

回答

1

如果使用彭博/路透社或任何其它數據提供者,那麼你可以簡單地得到任何你在Excel中想要的,然後讀給R和然後進行下載歷史數據。如果你沒有獲得這樣的商業數據提供者,然後的方式之一是網頁抓取(我相信維基百科是足夠可靠的,但做仔細檢查):

library(rvest) 
url <- "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies" 
SP500 <- url %>% 
    html() %>% 
    html_nodes(xpath='//*[@id="mw-content-text"]/div/table[1]') %>% 
    html_table() 
SP500 <- SP500[[1]] 
Tix <- SP500$`Ticker symbol` 
Tix 

截至今日,S &的成分P500:

> Tix 
    [1] "MMM" "ABT" "ABBV" "ACN" "ATVI" "AYI" "ADBE" "AMD" "AAP" 
[10] "AES" "AET" "AMG" "AFL" "A"  "APD" "AKAM" "ALK" "ALB" 
[19] "ARE" "ALXN" "ALGN" "ALLE" "AGN" "ADS" "LNT" "ALL" "GOOGL" 
[28] "GOOG" "MO" "AMZN" "AEE" "AAL" "AEP" "AXP" "AIG" "AMT" 
[37] "AWK" "AMP" "ABC" "AME" "AMGN" "APH" "APC" "ADI" "ANSS" 
[46] "ANTM" "AON" "APA" "AIV" "AAPL" "AMAT" "ADM" "ARNC" "AJG" 
[55] "AIZ" "T"  "ADSK" "ADP" "AN" "AZO" "AVB" "AVY" "BHI" 
[64] "BLL" "BAC" "BK" "BCR" "BAX" "BBT" "BDX" "BBBY" "BRK.B" 
[73] "BBY" "BIIB" "BLK" "HRB" "BA" "BWA" "BXP" "BSX" "BMY" 
[82] "AVGO" "BF.B" "CHRW" "CA" "COG" "CPB" "COF" "CAH" "CBOE" 
[91] "KMX" "CCL" "CAT" "CBG" "CBS" "CELG" "CNC" "CNP" "CTL" 
[100] "CERN" "CF" "SCHW" "CHTR" "CHK" "CVX" "CMG" "CB" "CHD" 
[109] "CI" "XEC" "CINF" "CTAS" "CSCO" "C"  "CFG" "CTXS" "CLX" 
[118] "CME" "CMS" "COH" "KO" "CTSH" "CL" "CMCSA" "CMA" "CAG" 
[127] "CXO" "COP" "ED" "STZ" "COO" "GLW" "COST" "COTY" "CCI" 
[136] "CSRA" "CSX" "CMI" "CVS" "DHI" "DHR" "DRI" "DVA" "DE" 
[145] "DLPH" "DAL" "XRAY" "DVN" "DLR" "DFS" "DISCA" "DISCK" "DISH" 
[154] "DG" "DLTR" "D"  "DOV" "DOW" "DPS" "DTE" "DD" "DUK" 
[163] "DXC" "ETFC" "EMN" "ETN" "EBAY" "ECL" "EIX" "EW" "EA" 
[172] "EMR" "ETR" "EVHC" "EOG" "EQT" "EFX" "EQIX" "EQR" "ESS" 
[181] "EL" "ES" "RE" "EXC" "EXPE" "EXPD" "ESRX" "EXR" "XOM" 
[190] "FFIV" "FB" "FAST" "FRT" "FDX" "FIS" "FITB" "FE" "FISV" 
[199] "FLIR" "FLS" "FLR" "FMC" "FL" "F"  "FTV" "FBHS" "BEN" 
[208] "FCX" "GPS" "GRMN" "IT" "GD" "GE" "GGP" "GIS" "GM" 
[217] "GPC" "GILD" "GPN" "GS" "GT" "GWW" "HAL" "HBI" "HOG" 
[226] "HRS" "HIG" "HAS" "HCA" "HCP" "HP" "HSIC" "HSY" "HES" 
[235] "HPE" "HLT" "HOLX" "HD" "HON" "HRL" "HST" "HPQ" "HUM" 
[244] "HBAN" "IDXX" "INFO" "ITW" "ILMN" "IR" "INTC" "ICE" "IBM" 
[253] "INCY" "IP" "IPG" "IFF" "INTU" "ISRG" "IVZ" "IRM" "JEC" 
[262] "JBHT" "SJM" "JNJ" "JCI" "JPM" "JNPR" "KSU" "K"  "KEY" 
[271] "KMB" "KIM" "KMI" "KLAC" "KSS" "KHC" "KR" "LB" "LLL" 
[280] "LH" "LRCX" "LEG" "LEN" "LVLT" "LUK" "LLY" "LNC" "LKQ" 
[289] "LMT" "L"  "LOW" "LYB" "MTB" "MAC" "M"  "MNK" "MRO" 
[298] "MPC" "MAR" "MMC" "MLM" "MAS" "MA" "MAT" "MKC" "MCD" 
[307] "MCK" "MDT" "MRK" "MET" "MTD" "KORS" "MCHP" "MU" "MSFT" 
[316] "MAA" "MHK" "TAP" "MDLZ" "MON" "MNST" "MCO" "MS" "MOS" 
[325] "MSI" "MUR" "MYL" "NDAQ" "NOV" "NAVI" "NTAP" "NFLX" "NWL" 
[334] "NFX" "NEM" "NWSA" "NWS" "NEE" "NLSN" "NKE" "NI" "NBL" 
[343] "JWN" "NSC" "NTRS" "NOC" "NRG" "NUE" "NVDA" "ORLY" "OXY" 
[352] "OMC" "OKE" "ORCL" "PCAR" "PH" "PDCO" "PAYX" "PYPL" "PNR" 
[361] "PBCT" "PEP" "PKI" "PRGO" "PFE" "PCG" "PM" "PSX" "PNW" 
[370] "PXD" "PNC" "RL" "PPG" "PPL" "PX" "PCLN" "PFG" "PG" 
[379] "PGR" "PLD" "PRU" "PEG" "PSA" "PHM" "PVH" "QRVO" "PWR" 
[388] "QCOM" "DGX" "RRC" "RJF" "RTN" "O"  "RHT" "REG" "REGN" 
[397] "RF" "RSG" "RAI" "RHI" "ROK" "COL" "ROP" "ROST" "RCL" 
[406] "CRM" "SCG" "SLB" "SNI" "STX" "SEE" "SRE" "SHW" "SIG" 
[415] "SPG" "SWKS" "SLG" "SNA" "SO" "LUV" "SPGI" "SWK" "SPLS" 
[424] "SBUX" "STT" "SRCL" "SYK" "STI" "SYMC" "SYF" "SNPS" "SYY" 
[433] "TROW" "TGT" "TEL" "FTI" "TSO" "TXN" "TXT" "TMO" "TIF" 
[442] "TWX" "TJX" "TMK" "TSS" "TSCO" "TDG" "RIG" "TRV" "TRIP" 
[451] "FOXA" "FOX" "TSN" "UDR" "ULTA" "USB" "UA" "UAA" "UNP" 
[460] "UAL" "UNH" "UPS" "URI" "UTX" "UHS" "UNM" "VFC" "VLO" 
[469] "VAR" "VTR" "VRSN" "VRSK" "VZ" "VRTX" "VIAB" "V"  "VNO" 
[478] "VMC" "WMT" "WBA" "DIS" "WM" "WAT" "WEC" "WFC" "HCN" 
[487] "WDC" "WU" "WRK" "WY" "WHR" "WFM" "WMB" "WLTW" "WYN" 
[496] "WYNN" "XEL" "XRX" "XLNX" "XL" "XYL" "YUM" "ZBH" "ZION" 
[505] "ZTS" 
+0

我很感謝您的幫助。 – Jabda100su

+0

謝謝,我正在尋找一種比我自己手動輸入更簡單的方式 – user113156

相關問題