Doubts about calling multiple objects, one by one in iterations of an R looping

Asked

Viewed 57 times

0

I imported some objects as follows, in this example I put only two, but I intend to work with many, so I need to automate some processes:

library(quantmod)

#escolhendo a data que deseja
start <- as.Date("2014-01-01")
end<- as.Date("2018-01-12")

#primeiro termo refere a quais ativos vc quer baixar
#segundo de onde vc quer
#terceiro a data inicial
#quarto a data final
getSymbols(ativos, src = "yahoo", from = start, to = end)

After importing the objects, I need to transfer them to data.frame and change their headers, in a for, but I’m not getting to call every object in every iteration.

inserir a descrição da imagem aqui

I’d like a way to change all the headers into one for, as well as turning everyone into data.frame in a for, but I can’t call every object in every iteration.

I tried to do it but it didn’t work:

#cabeçalho dos ativos importados
cabecalho<-c("abertura","máxima", "mínima", "fechamento", "volume", "ajuste")

ativos2<-list(BVSP,PETR4.SA)


for(i in 1:NROW(ativos2)){

  colnames(ativos2[i])<-cabecalho

}

As suggested by the colleague in the comment I repeated this is as follows

for(i in 1:NROW(ativos2)){

 colnames(ativos2[[i]])<-cabecalho

}

And the following error appears

Error in colnames<-(*tmp*, value = header) : Attempt to set 'colnames' on an Object with Less than two Dimensions

  • 1

    On the last line, try using 2 brackets instead of only 1: ativos2[[i]]. If it doesn’t work, edit the question and add the error message. You can also provide more information describing the class() of objects BVSP, PETR4.

  • 1

    Welcome to Stackoverflow! Take a look at how to improve your question so that we can help.

  • 1

    In particular, share the result of dput(ativos2) can help

  • 1

    It seems that the problem is that the elements of its object ativos2 do not have two dimensions (rows and columns), and then the function colnames() no columns to extract. Take a look at class() of your objects within the list: for(i in 1:NROW(ativos2) class(ativos2[[i]])

No answers

Browser other questions tagged

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