rahul yadav
rahul yadav

Reputation: 432

return the output of `getSymbols()` as a data frame

If i try to take input from user and save the data frame into another for further use in code

 comp.name <- readline(prompt = "Enter the company name: ")
 getSymbols(comp.name , src = "yahoo", verbose = TRUE, from = "2018-03-01")
 tyu2 <- as.data.frame(comp.name)

I don't get the data back, but only a data frame with a single factor value. Please suggest something. The further code goes like

tyu <- tyu2$(comp.name).Open
x <- row.names(tyu2)
final <- length(tyu)
final <- as.numeric(final)
p <- ggplot(data = tyu2 , aes(x= x ,y=tyu))+geom_bar(stat = "identity", fill = "blue")+ theme(axis.text.x = element_text(angle = 90))
p

It may be a foolish mistake. I am quite new to R. Thanks.

Upvotes: 2

Views: 1748

Answers (2)

rosscova
rosscova

Reputation: 5580

getSymbols auto assigns the returned value to the global environment by default, meaning you'll get an object with the same name as the company you queried. To stop that behaviour, set auto.assign to FALSE. This way, you can assign the returned values to an object yourself:

comp.name <- readline(prompt = "Enter the company name: ")
tyu2 <- getSymbols(comp.name ,
                   src = "yahoo",
                   verbose = TRUE,
                   from = "2018-03-01",
                   auto.assign = FALSE)
tyu2 <- as.data.frame(tyu2)

Upvotes: 3

Wimpel
Wimpel

Reputation: 27732

To prevent character-vectors to be converted to factor in the creation of a data.frame, use stringsAsFactors = FALSE when creating the data.frame.

 tyu2 <- as.data.frame(comp.name, stringsAsFactors = FALSE)

Upvotes: 0

Related Questions