How to transform data imported from Excel (.csv) into time series

Asked

Viewed 1,049 times

4

I imported an Excel database to R and I need to turn it into time series to be able to analyze it. However, when I am going to make the transformation to time series R changes the original values to totally different ones. Below are the steps I used

to import the data:

variavel=read.table("dados.csv", header=T, sep=";", dec=",")

after importing the data, the values appeared correctly in the assigned variable, being values of type 5.547,18...

to transform the data into time series:

library(tseries)# carregando o pacote séries temporais

ts.variavel=ts(variavel, start=c(2000,1), frequency=12) # fazendo a transformação

After the above transformation, when I go to check the data they appear totally changed, for example, the value of 5.547,18 turned 15, the value 5.344,47 turned 7, the value 5.053,42 turned 2 and so on... Strange, isn’t it?! What am I doing wrong?

  • Daniel’s answer seems plausible but, if it doesn’t solve your problem, could you post an example of your data? For example: dput(head(variavel)).

  • Thanks for helping, I was able to solve the problem as Daniel answered.

  • This is also worth taking a look at: http://answall.com/questions/9284/erro-ao-converter-n%C3%Bameros-how-convert-factors-to-n%C3%Bameros

1 answer

1


I believe the problem is reading the data.

Apparently his data has a thousands separator that R is not understanding. That’s why he reads the variables as factor, which is then transformed into a whole.

The easiest way to solve this problem is: in excel itself format the columns to not have thousands separator.

Check that the data is being read correctly with:

str(variavel)

This function should indicate that your data is of the numeric type and not the factor type. Example:

'data.frame':   100 obs. of  1 variable:
 $ x: num  0.426 0.664 0.844 0.76 0.781 ...
  • 1

    It worked, thank you.

Browser other questions tagged

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