Graph generation error in R

Asked

Viewed 42 times

3

Hello,

I’m trying to create a chart from a list, but I’m not getting it.

The download part works perfectly:

    # download adj. price data - Asset[1],Asset[2]...
symbols= c("TIET4.SA","BVMF3.SA", "CCRO3.SA","ITUB4.SA","WIZS3.SA")

    Asset = list()
    for (symbol in symbols)
      Asset[[symbol]] = as.zoo(download_data(symbol, start_date, end_date))

When I try to generate the graph with this code:

# Visualiza gráficos da carteira
for(i in 1:length(symbols)){
  plot(Asset[i], main=asset1, col=2, xlab="Período",
       ylab="Preço",type="l")
}

I get the following error:

Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' is a list, but does not have components 'x' and 'y'

I have seen some posts showing the identical error, but I could not understand, nor reproduce the answers that were given. I am not experienced in R!

Could someone help?

Thank you,

1 answer

6


The object Asset is a list. To select each element from this list with inter i you must use double brackets: [[ ]]:

# Visualiza gráficos da carteira
for(i in 1:length(symbols)){
      plot(Asset[[i]], main=asset1, col=2, xlab="Período",
      ylab="Preço",type="l")
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.