循环读取字符向量按顺序生成vs字母

时间:2017-10-19 13:37:48

标签: r loops

好的,所以我从按日期排序的排序数据框中提取了一份代码清单。因此,我的符号字符向量按排序顺序:

i

我希望循环按照生成的顺序读取此字符向量与字母。

如果我用以下内容说明数据加载:

> tickers
  [1] "SPPI" "ZGNX" "ARDM" "PRTK" "GTXI" "HEB"  "FCSC" "ACOR" "ALKS" "MNKD" "HRTX" "CTIC"
 [13] "ARLZ" "JAZZ" "VVUS" "DEPO" "OREX" "PLX"  "PTIE" "DRRX" "SGEN" "PCRX" "PSDV" "ALIM"
 [25] "INCY" "ATRS" "INSY" "CRIS" "CORT" "EBS"  "RGEN" "ARNA" "AMRN" "HALO" "NAVB" "SUPN"
 [37] "EXEL" "IPXL" "IMGN" "DVAX" "SCMP" "TTNP" "ENDP" "AVDL" "AVEO" "TBPH" "DCTH" "ABBV"
 [49] "AMAG" "VNDA" "BMRN" "MDCO" "OMER" "BDSI" "EGRX" "ACRX" "KERX" "NKTR" "PGNX" "AEZS"
 [61] "ENTA" "BCRX" "ADMS" "VRTX" "NBIX" "RMTI" "ADMP" "AMGN" "MNTA" "PTX"  "EBIO" "NYMX"
 [73] "VTL"  "TTPH" "MACK" "LPTX" "GWPH" "SPHS" "RPRX" "OTIC" "NEOT" "CHRS" "ZFGN" "NEOS"
 [85] "RDHL" "PTLA" "OPK"  "CHMA" "ACAD" "NLNK" "AZN"  "ICPT" "AAAP" "DERM" "OCUL" "MRNS"
 [97] "RVNC" "CLVS" "GALE" "LPCN" "TSRO" "AMPE" "CYTR" "RARE" "MCRB" "ADMA" "IONS" "VTVT"
[109] "AUPH" "EARS" "ACRS" "KMDA" "RIGL" "KPTI" "TNXP" "AERI" "NVAX" "VICL" "SRPT" "GILD"
[121] "ITCI" "GNCA" "ABUS" "CEMP" "TENX" "ALNY" "PLXP" "PTN"  "INNL" "ANTH" "CRBP" "BSTC"
[133] "REPH" "NOVN" "CERC" "HTBX" "LXRX" "HZNP" "SGYP" "OPHT" "AKAO" "LIFE" "PRTO" "VCEL"
[145] "IRWD" "PBMD" "AMPH" "PFE"  "AGRX" "EGLT" "ADHD" "FGEN" "AGN"  "GEMP" "OCRX" "CATB"
[157] "DMTX" "AVIR" "JNJ"  "TCON" "SAGE" "ZSAN" "AXON" "MRK"  "VRX"  "ARDX" "XBIT" "CDTX"
[169] "TRVN" "CELG" "CMRX" "ARGS" "LJPC" "NDRM" "PBYI" "SCYX" "PTCT" "GALT" "KURA" "AKCA"
[181] "TGTX" "NVS"  "CPRX" "LLY"  "GNMX" "BLRX" "XENE" "FOMX" "SNY"  "REGN" "RTTR" "CARA"
[193] "NVCR" "BMY"  "ONCE" "GERN" "MESO" "OMED" "MTFB" "EIGR" "ACHN" "AKTX" "XOMA" "CAPR"
[205] "RDUS" "NTRP" "BPMX" "TXMD" "BTX"  "GSK"  "CORI" "FOLD" "BLPH" "SBPH" "NVO"  "RETA"
[217] "ECYT" "IMDZ" "MTNB" "ARQL" "LOXO" "ZYME" "RNN"  "PIRS" "FPRX" "CALA" "BGNE" "BLUE"
[229] "CLSN" "CRVS" "GLYC" "JUNO" "IOVA" "RGLS" "XLRN" "ALDX" "EPZM" "SELB" "IMUC" "BLCM"
[241] "GBT"  "STML" "AGIO" "RARX" "ALDR" "ITEK" "IMRN" "QURE" "SVRA" "KDMN" "CBAY" "BVXV"
[253] "CYTX" "NVIV" "MYOK" "ZYNE" "ESPR" "GLPG" "ABIO" "CVM"  "STDY" "CLLS" "INSM" "VSTM"
[265] "VYGR" "VRNA" "UTHR" "ARRY" "BPMC" "IDRA" "INO"  "EPIX" "AGEN" "FENC" "MRTX" "INVA"
[277] "NBRV" "VSAR" "IPCI" "PRQR" "AZRX" "PRTA" "BHVN" "MYL"  "FLXN" "ANAB" "RXDX"

如果我们打印代码[i]

,我们会看到什么
# Note function is store list of commands to perform over a directory of files

genCHART = function(x){  
  next.symbol <- tickers[i]  # specify to start from first position in vector
  date.list <- dates[i] # specify to start from first position in vector
          next.file <- fread(paste0("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=",next.symbol,"&outputsize=full&apikey=6RSYX9BPXKZVXUS9&datatype=csv"))
          new.df <- data.frame(next.file)
          head(new.df)
}

# Loop calls function in order to process multiple files
        for (i in 1:length(tickers)){
            genCHART(tickers[[i]])
        }

    # loop will do nothing but process and load all tickers.. but its too illustrate the point.

它按字母顺序给我第一个自动收报机。所以它返回以A开头的订单,而不是我上面的订单。我希望它循环我的字符列表作为股票代码矢量的顺序。

反正过来了吗?

编辑帖子

> next.symbol
[1] "ANTH"

我想从矢量中的第一个开始...

IF I take a vector of dates: 
> dates
  [1] "2009-07-05" "2009-07-16" "2009-07-16" "2009-09-04" "2009-10-09" "2009-11-02"
  [7] "2009-11-02" "2009-12-01" "2009-12-18" "2010-01-22" "2010-01-27" "2010-03-15"
 [13] "2010-03-15" "2010-03-19" "2010-04-09" "2010-04-30" "2010-10-11" "2010-10-28"
 [19] "2011-01-19" "2011-01-28" "2011-02-01" "2011-02-25" "2011-04-29" "2011-06-22"
 [25] "2011-06-24" "2011-06-24" "2011-08-19" "2011-10-31" "2011-11-11" "2011-11-11"
 [31] "2011-11-16" "2011-11-23" "2011-12-08" "2012-01-05" "2012-01-30" "2012-02-17"

即使它包含在一个函数中,上述工作也不行吗?

我怎样才能使它工作,以便我读取向量的开头,以及当我将代码放入函数然后在循环中运行函数来处理多个文件时,它是如何工作的?

1 个答案:

答案 0 :(得分:0)

print(i) =3

i=1

是答案