2

First of all, sorry for any mistakes regarding my post, I'm new to this site.

I´m getting started with R now and I´m trying to do some analysis with time series data. So, I got a times series at hand and already loaded it into R. I can also plot this times series and add labels to the axes and so on. So far so good.

My problem: When I plot the time series, R would set the range of values on the y-axis to the interval of [0:170] approximately. This is somehow strange, since the times series contains the daily EUR/USD exchange rates for this year. That means the values are in a range of about 1.05 to 1.2.

enter image description here

The relative values are correct. If the plot shows a maximum around day 40, the corresponding value in the data set appears to be a maximum. But it is around 1.4 and not 170.

I hope one can understand my problem. I would like to have the y-axis on a scale from 1 to 1.2 for example. The ylim=c(1, 1.2) command will scale the axis to that range but not the values. It just ignores them.

Does anyone know how to adjust that? I´d really appreciate it.

Thank you very much in advance.


Thanks a lot for the input so far. The "critical code" is the following:

> FRB <- read.csv("FRB_H10.csv", header=TRUE, sep=",")
> attach(FRB)
> str(FRB)
'data.frame':   212 obs. of  2 variables:
 $ Date: Factor w/ 212 levels "2015-01-01","2015-01-02",..: 1 2 3 4 5 6 7  8 9 10 ...
 $ Rate: Factor w/ 180 levels "1.0524","1.0575",..: 180 179 177 178 174 173 175 176 171 172 ...
> plot.ts(Rate)

The result of this last plot is the one shown above.

Changing the variable to numeric yields this:

> as.numeric(Rate)
  [1] 180 179 177 178 174 173 175 176 171 172 170 166 180 167 169 160 123 128 150 140 132 128 138 165
 [25] 161 163 136 134 134 129 159 158 180 156 140 155 151 142 131 148 104 100  96 104  65  53  27  24
 [49]  13   3   8   1   2   7  10   9  21  42  36  50  39  33  23  15  19  29  51  54  26  23  11   6
 [73]   4  12   5  16  20  18  17  14  22  30  34  49  92  89  98  83  92 141 125 110  81 109 151 149
 [97] 162 143  85  69  77  61 180  30  32  38  52  37  78 127 120  73 105 126 131 106 122 119 107 112
[121] 157 137 152  96  93  99  87  94  86  70  71 180  67  43  66  58  84  57  55  47  35  25  26  41
[145]  31  48  48  75  63  59  38  60  46  44  28  40  45  52  62 101  82  74  68  60  64 102 144 168
[169] 159 154 108  91  98 118 111  72  76 180  95  90 117 139 131 116 130 133 145 103  79  88 115  97
[193] 106 113  89 102 121 102 119 114 124 148 180 153 164 161 147 135 146 141  80  56

So, it remains unchanged. This is very strange. The data excerpt shows that "Rate" takes on values between 1.1 and 1.5 approximately, so really not the values that are shown above. :/

The data set can be found under this link:

https://www.dropbox.com/s/ndxstdl1aae5glt/FRB_H10.csv?dl=0

It should be alright. I got it from the data base from the Federal Reserve System, so quite a decent source.

(Had to remove the link to the data excerpt because my reputation only allows for 2 links to be posted at a time. But the entire data set should be even better, I guess.

  • perhaps your currency column is a factor right now. try the str() function to have a look at your data. Maybe it'll be as simple as changing your values to numeric – s_scolary Oct 30 '15 at 18:32
  • A question to SO should have complete minimal self contained code including all inputs, library statements, etc. so readers can copy and paste it into an R session to reproduce the result on their own machine. These 3 links contain more on how to ask questions: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example http://stackoverflow.com/help/mcve http://stackoverflow.com/help/how-to-ask – G. Grothendieck Oct 30 '15 at 19:33
  • I´ve just edited the question and included the code. :) – WiWiStudent Oct 30 '15 at 20:11
  • The discussion [here](http://stackoverflow.com/questions/3418128/how-to-convert-a-factor-to-an-integer-numeric-without-a-loss-of-information) might be relevant to your problem as `as.numeric` does not do what you think it should do. – horseoftheyear Oct 30 '15 at 20:17

1 Answers1

1

@BlankUsername

Thanks very much for the link. I got it working now using this code:

FRB <- read.csv("FRB_H10.csv", header=TRUE, sep=",")
> attach(FRB)
> as.numeric(paste(Rate))
  [1]     NA 1.2015 1.1918 1.1936 1.1820 1.1811 1.1830 1.1832 1.1779 1.1806 1.1598 1.1517     NA
 [14] 1.1559 1.1584 1.1414 1.1279 1.1290 1.1370 1.1342 1.1308 1.1290 1.1337 1.1462 1.1418 1.1432
 [27] 1.1330 1.1316 1.1316 1.1300 1.1410 1.1408     NA 1.1395 1.1342 1.1392 1.1372 1.1346 1.1307
 [40] 1.1363 1.1212 1.1197 1.1190 1.1212 1.1070 1.1006 1.0855 1.0846 1.0707 1.0576 1.0615 1.0524
 [53] 1.0575 1.0605 1.0643 1.0621 1.0792 1.0928 1.0908 1.0986 1.0919 1.0891 1.0818 1.0741 1.0768
 [66] 1.0874 1.0990 1.1008 1.0850 1.0818 1.0671 1.0598 1.0582 1.0672 1.0596 1.0742 1.0780 1.0763
 [79] 1.0758 1.0729 1.0803 1.0876 1.0892 1.0979 1.1174 1.1162 1.1194 1.1145 1.1174 1.1345 1.1283
 [92] 1.1241 1.1142 1.1240 1.1372 1.1368 1.1428 1.1354 1.1151 1.1079 1.1126 1.1033     NA 1.0876
[105] 1.0888 1.0914 1.0994 1.0913 1.1130 1.1285 1.1271 1.1108 1.1232 1.1284 1.1307 1.1236 1.1278
[118] 1.1266 1.1238 1.1244 1.1404 1.1335 1.1378 1.1190 1.1178 1.1196 1.1156 1.1180 1.1154 1.1084
[131] 1.1090     NA 1.1076 1.0952 1.1072 1.1025 1.1150 1.1020 1.1015 1.0965 1.0898 1.0848 1.0850
[144] 1.0927 1.0884 1.0976 1.0976 1.1112 1.1055 1.1026 1.0914 1.1028 1.0962 1.0953 1.0868 1.0922
[157] 1.0958 1.0994 1.1042 1.1198 1.1144 1.1110 1.1078 1.1028 1.1061 1.1200 1.1356 1.1580 1.1410
[170] 1.1390 1.1239 1.1172 1.1194 1.1263 1.1242 1.1104 1.1117     NA 1.1182 1.1165 1.1262 1.1338
[183] 1.1307 1.1260 1.1304 1.1312 1.1358 1.1204 1.1133 1.1160 1.1252 1.1192 1.1236 1.1246 1.1162
[196] 1.1200 1.1276 1.1200 1.1266 1.1249 1.1282 1.1363     NA 1.1382 1.1437 1.1418 1.1360 1.1320
[209] 1.1359 1.1345 1.1140 1.1016
Warning message:
NAs introduced by coercion 
> Rate <- cbind(paste(Rate))
> plot(Rate)
Warning message:
In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
> plot.ts(Rate, ylab="EUR/USD")

Despite the warning message, I get the following output (shown below). Like I intended to plot it.

enter image description here

Nevertheless, I do not really understand why it works the way it did. Why I have to use the paste() command and what it does exactly. I get the basic idea of what the classes do, but am very new to this whole world of R.

One thing I came to realize already is that R is such a powerful program. And yet confusing if you are a beginner. :D

  • Do the NAs persist when you use `as.numeric(levels(Rate))[Rate]`? Would be nice if that can be sorted out as well so you have a fully functioning code. – horseoftheyear Oct 30 '15 at 22:00
  • Yes, still "NAs introduced by coercion". What does NA mean, by the way? – WiWiStudent Oct 30 '15 at 22:05
  • NA is for not available, if I´m not mistaken? Why does R introduce those? I mean there have been some NAs in the data set from the beginning. But obviously, R created some more. – WiWiStudent Oct 30 '15 at 22:13
  • I'm not quite sure why this happens. Another thing you can try is to include `stringsAsFactors = FALSE` in the `read.csv` command. That should prevent R from loading the data as factors in the first place. – horseoftheyear Oct 31 '15 at 10:29
  • `stringsAsFactors = False` works fine, as well. It´s even easier to do it this way and load the data as numeric from the get-go. The output is, of course, the same as shown above. The scale on the y-axis is correct. – WiWiStudent Oct 31 '15 at 15:59