Data.frame for ts daily data in R

Asked

Viewed 435 times

2

I’m not managing to turn the daily exchange rate to ts.

dados <- read.table("C:/Econometria/Cambio/cambio.txt", header=T, dec=",")
ts(dados), start=c(1994,01,01), freq=1) # Quando faço isso ele muda os dados, acaba aparecendo valores que não estão no arquivo

I also used xts(dados, as.Date(dados, format='%m/%d/%Y')

Could someone help me?

str(dados)
'data.frame':   5103 obs. of  1 variable:
 $ X1: Factor w/ 4142 levels "-","0,829","0,831",..: 86 81 70 65 74 77 74 74 77 83 ...

dput(dados)
... , "3,9245", "3,9552"), class = "factor")), .Names = "X1", class = "data.frame", row.names = c(NA, 
-5103L))
  • Diogo, put the result of str(dados) and if possible dput(dados) in your question, to know what is in your database.

1 answer

2


Diogo, the problem is that your values are not as a number, but as factor (factors).

This is happening at the time of reading the database because the R is interpreting your data as text (and not as number) and transforming into factor.

If you look at the information you gave with the str, the first level is a "-" dash. That’s probably what’s causing the problem. I suggest you replace these values with zero before re-reading.

PS: Out of curiosity, if you want to understand why factors turned into numbers turn into "other" numbers, I suggest reading this question: Error while converting numbers. How to convert factors to numbers? .

  • An alternative could be to replace the "missing" values by NA using the argument na.strings of function read.table. In your case, it would be read.table(..., na.strings='-')

Browser other questions tagged

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