1

I am trying to write a small program in R that will randomly select a stock from the NYSE and display that stock's YTD information in a data.frame.

I currently have:

Stock.List<-data.frame(StockListNew)
colnames(Stock.List)<-c("Number", "Ticker", "Company Name")
# Renamed Columns to number, ticker and company name

random.stock<-sample.rows(data.frame(Stock.List),1,replace=TRUE)
Stock.List[sample.int(3284,size=1,replace=TRUE),2]
#Selects a Random Stock Ticker and Name from Stock.List

So far this has worked for me. When I run...

Stock.List[sample.int(3284,size=1,replace=TRUE),2]

I successfully return the random ticket in the console, for example:

> Stock.List[sample.int(3284,size=1,replace=TRUE),2]
[1] WPP
3284 Levels: A AA AA-B AAC AAN AAP AAT AAV AB ABB ABBV ... ZX

I end up getting the second line which I don't understand why R is relisting the Stock.List as levels.

Ultimately my goal is to then have R open quantmod and use the getSymbols() function to grab the random stock I selected and download the data.frame from January 1st, 2015 to Dec 18th, 2015.

I know that I need to get the randomly selected stock into a string(?) to properly use the getSymbols function.

Sorry if my language is not proper I am brand new to R and programming languages.

Dan Foley
  • 11
  • 1
  • 2
    Try googling "factors in R" .. Or check out that : http://www.stat.berkeley.edu/~s133/factors.html –  Dec 22 '15 at 17:32
  • You have the stocks stored as a factor (try `str(Stock.List$Ticker)`) To convert it to a character vector try `Stock.List$Ticker <- as.character(Stock.List$Ticker)` – jeremycg Dec 22 '15 at 17:33
  • `Ticker` is a factor. Change it to a character. `Stock.List$Ticker <- as.character(Stock.List$Ticker)` – mrp Dec 22 '15 at 17:33
  • Thank you guys-- I changed it to to a character and it worked! – Dan Foley Dec 22 '15 at 18:12
  • Although converting your columns to characters after creating or reading in a data.frame works, it is more efficient to use the stringsAsFactors=FALSE argument when reading in or creating a data.frame. – Daniel Bachen Dec 22 '15 at 19:15

0 Answers0