Question

deflator <- nominalGDP/realGDP
WARNINING MSG  In Ops.factor(nominalGDP, realGDP) 

The question is to get:

deflator = nominal/real

Use nominalGDP and realGDP to calculate deflator values. You should end up with a numeric vector called deflator that looks like this:

> deflator
[1] 0.9628929 0.9596746 0.9747274 0.9832532 0.9851884
[6] 0.9797770 0.9913502 1.0100561 1.0176906 1.0092516
[11] 1.0185932 1.0241043 1.0197975 1.0174097 1.0297328
... [71] 1.4497201

nominalGDP contains

> nominalGDP
 [1] 21382 21585 20557 23720 22748 23110 22005 25211 24219
[10] 24436 23502 26554 24777 25538 24624 27279 25550 25505
[19] 24664 28142 26563 26794 26262 29487 28635 28204 27694
[28] 31593 30016 30688 29517 33754 32365 31875 31847 35178
[37] 33920 33715 33882 37685 36742 36890 36528 40263 38357
[46] 39229 38836 42192 40316 40541 40356 44830 42908 43930
[55] 43094 48599 46076 45562 43637 49231 46665 45711 43757
[64] 49409 48078 47773 46120 53168 51037 50191 48944

and the realGDP contains

> realGDP
 [1] 22,206 22,492 21,090 24,124 23,090 23,587 22,197 24,960 23,798 24,212
[11] 23,073 25,929 24,296 25,101 23,913 26,492 24,774 24,896 23,835 26,852
[21] 25,734 25,883 25,244 28,225 27,260 26,894 25,968 28,891 27,372 27,543
[31] 26,753 30,045 28,556 28,490 28,093 31,702 30,214 29,870 29,387 32,535
[41] 31,427 31,310 30,577 33,655 32,125 32,372 31,760 34,753 32,968 32,922
[51] 32,103 35,658 34,033 34,470 33,195 36,489 34,400 33,685 32,680 36,487
[61] 34,328 34,194 32,073 36,495 35,201 34,843 33,207 37,279 35,666 35,028
[71] 33,761
71 Levels: 21,090 22,197 22,206 22,492 23,073 23,090 23,587 ... 37,279

The first way I tried it was simply divide the two lists using /:

nominalGDP/realGDP  

and got me a warning message.

I think both are numeric vectors but it seems they don't correspond to each other. And I also tried mapply("/", nominalGDP, realGDP).

Please let me know what I'm missing here to work out the right result.

I don't want the answer itself, I want to know why it's not working.

Was it helpful?

Solution

Your realGDP is of factor class. I think this is because you have comma as a decimal point and R treats it as factor (not numeric). You should use parameter dec=',' when reading data or change commas to points in your input file.

OTHER TIPS

The clue is at the end of the printed representation of `realGDP". It's a factor variable. You need to do this:

 readGDP <- as.numeric(as.character(realGDP))

... then you will have two numeric vectors.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top