error message trying to import stock data

## 1) Import your assigned stocks

library(tidyquant)
library(tidyverse)
stocks<-tq_get(c("AMD", "VLGEA", "UL"), get = "stock.prices", from = 2000-01-01)

stockdata<-c("AMD", "VLGEA" , "UL")%>%
  select(stocks, date, adjusted)

head(stockdata, n = 6)%>%
  kable(caption="First six data points.")

Error in UseMethod("select") : no applicable method for 'select' applied to an object of class "character"

I think this is an issue with the middle chunk of your code - have you copy-pasted the vector accidentally?

The below code chunk runs fine and is (I'm guessing) what you're trying to do:

library(tidyquant)
library(tidyverse)

stocks <- tq_get(c("AMD", "VLGEA", "UL"), get = "stock.prices", from = 2000-01-01)

stockdata <- stocks %>%
  select(date, adjusted)

head(stockdata, n = 6) %>%
  knitr::kable(caption = "First six data points.")

I copied the code directly from the example and inserted my own stocks. I need the "symbol" as that goes into the table. I've manged to get it to work like this:

library(tidyquant)
stocks<-tq_get(c("AMD", "VLGEA", "UL"), get = "stock.prices", from=2000-01-01)

stockdata<-c("AMD", "VLGEA","UL") %>%
tq_get(get = "stock.prices", from = "2000-01-01")%>%
select(symbol, date, adjusted)

head(stockdata, n = 6 )%>%
kable(caption = "First six data points.")

Also, any help on the table creation for each stock would be helpful. each time i run the data it only creates the table and retrieves stock data for "AMD"

Yes, that will also work!

So your tq_get() function seems to be importing stock data.

To unpack what seems to be going on, this first line appears to be a function to import stock data, with options given to specify what to import and from what date:

stocks<-tq_get(c("AMD", "VLGEA", "UL"), get = "stock.prices", from=2000-01-01)

The second chunk is doing much the same. The first line is specifying a vector of symbols - c("AMD", "VLGEA","UL") - which is then "piped" into the first argument of tq_get(). So, effectively, everything up to select() is just repeating the above line. select() is selecting the symbol, date and adjusted columns.

stockdata<-c("AMD", "VLGEA","UL") %>%
    tq_get(get = "stock.prices", from = "2000-01-01")%>%
    select(symbol, date, adjusted)

The next bit is taking the data from above (stockdata, which is your imported stock data from AMD, VLGEA and UL containing the columns symbol, date and adjusted). head() is taking the top 6 rows of the data, and then kable() is formatting it as a table.

head(stockdata, n = 6 )%>%
    kable(caption = "First six data points.")

Your dataframe contains data for AMD, VLGEA and UL, but your table only contains AMD because that data is in the first 6 lines.

If you would just like to see your data, consider some of these options:

head(stockdata) #this prints the top few lines
tail(stockdata) #this prints the bottom few lines
dplyr::glimpse(stockdata) #this is a different way to print the data
View(stockdata) #this lets you see the whole data set in an RStudio tab

To tabulate your data you will have to reduce it a bit as its very big (1648 observations would make a big table!). To pull the top 6 rows of each symbol, I'd suggest this pipeline:

stockdata %>%
  group_by(symbol) %>%
  slice_head(n = 6) %>%
  knitr::kable(caption = "First six data points of each company.")

This prints:

Table: First six data points of each company.

symbol date adjusted
AMD 2000-01-03 15.500000
AMD 2000-01-04 14.625000
AMD 2000-01-05 15.000000
AMD 2000-01-06 16.000000
AMD 2000-01-07 16.250000
AMD 2000-01-10 17.500000
UL 2000-01-03 8.042123
UL 2000-01-04 7.925084
UL 2000-01-05 8.259480
UL 2000-01-06 8.426676
UL 2000-01-07 8.894826
UL 2000-01-10 8.577147
VLGEA 2000-01-03 1.709273
VLGEA 2000-01-04 1.733808
VLGEA 2000-01-05 1.733808
VLGEA 2000-01-06 1.701095
VLGEA 2000-01-07 1.701095
VLGEA 2000-01-10 1.701095

Firstly, thank so much for your help. As you can tell i'm very new to this coding -
Thats exactly what I need, where would I include the pipeline code? i've tried adding it to the bottom of the chuck but I cant run it or knit it?

this was the example we were told to use:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.