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.
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
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 theclass()
of objectsBVSP
,PETR4
.– Willian Vieira
Welcome to Stackoverflow! Take a look at how to improve your question so that we can help.
– Tomás Barcellos
In particular, share the result of
dput(ativos2)
can help– Tomás Barcellos
It seems that the problem is that the elements of its object
ativos2
do not have two dimensions (rows and columns), and then the functioncolnames()
no columns to extract. Take a look atclass()
of your objects within the list:for(i in 1:NROW(ativos2) class(ativos2[[i]])
– Willian Vieira