Bart
Bart

Reputation: 759

Multiplying two numeric vectors returns all 1s

Probably I'm overlooking something very simple but I can't seem to find it. I'm assigning a new column to a dataframe which holds the multiplied value of two other columns. The results in the new column are all 1s, but obviously shouldn't be. On the other hand, dividing the columns doesn't seem to be a problem.

A reproducible example:

testset<-
  read.table(text='"afd" "yr" "val1" "val2" "val3" "val4"
"1" "1" 2012 875.309027777778 514 0.587221179821412 1.70293585170774
"2" "1" 2013 929.445833333333 531 0.571308171984202 1.75036880100439
"3" "2" 2012 5983.97777777778 816 0.136364142766424 7.33330610021786
"4" "2" 2013 5515.6125 622 0.11277079381483 8.86754421221865
"5" "3" 2012 1289.25416666667 324 0.251308088332725 3.97917952674897
"6" "3" 2013 1032.18472222222 281 0.272238092611007 3.67325523922499
"7" "4" 2012 292.047916666667 105 0.359530042872531 2.78140873015873
"8" "4" 2013 276.289583333333 97 0.35108091600751 2.84834621993127
"9" "5" 2012 4605.56180555556 794 0.172400248552136 5.80045567450322
"10" "5" 2013 5728.63680555555 855 0.149250167015446 6.70016000649773
"11" "6" 2012 863.24375 352 0.407764319174046 2.45239701704545
"12" "6" 2013 819.725 342 0.417213089755711 2.3968567251462
"13" "7" 2012 8875.80902777778 519 0.0584735429047352 17.1017514986084
"14" "7" 2013 9065.16805555556 541 0.0596789818660284 16.7563180324502
"15" "8" 2012 811.765277777778 121 0.149057866001906 6.70880394857668
"16" "8" 2013 741.938194444444 115 0.154999433727914 6.45163647342995
"17" "9" 2012 628.202777777778 261 0.415470942238219 2.40690719455087
"18" "9" 2013 609.850694444444 261 0.427973604650501 2.33659269902086
"19" "10" 2012 27.2923611111111 6 0.219841734307015 4.54872685185185
"20" "10" 2013 39.0069444444444 5 0.128182303720847 7.80138888888889
"21" "11" 2012 15305.5229166667 2324 0.151840614179168 6.58585323436604
"22" "11" 2013 14206.5472222222 2317 0.163093816094575 6.1314403203376
"23" "12" 2012 1545.67986111111 202 0.130686829195531 7.65188050055005
"24" "12" 2013 1408.35 214 0.151950864486811 6.58107476635514
"25" "13" 2012 1067.82847222222 333 0.311847837609167 3.20669210877544
"26" "13" 2013 1151.11875 327 0.284071473946541 3.52024082568807')

testset$val5<-
  testset$val3 * testset$val4

testset$val6<-
  testset$val3 / testset$val4

And the output:

   afd   yr        val1 val2       val3      val4 val5        val6
1    1 2012   875.30903  514 0.58722118  1.702936    1 0.344828714
2    1 2013   929.44583  531 0.57130817  1.750369    1 0.326393027
3    2 2012  5983.97778  816 0.13636414  7.333306    1 0.018595179
4    2 2013  5515.61250  622 0.11277079  8.867544    1 0.012717252
5    3 2012  1289.25417  324 0.25130809  3.979180    1 0.063155755
6    3 2013  1032.18472  281 0.27223809  3.673255    1 0.074113579
7    4 2012   292.04792  105 0.35953004  2.781409    1 0.129261852
8    4 2013   276.28958   97 0.35108092  2.848346    1 0.123257810
9    5 2012  4605.56181  794 0.17240025  5.800456    1 0.029721846
10   5 2013  5728.63681  855 0.14925017  6.700160    1 0.022275612
11   6 2012   863.24375  352 0.40776432  2.452397    1 0.166271740
12   6 2013   819.72500  342 0.41721309  2.396857    1 0.174066762
13   7 2012  8875.80903  519 0.05847354 17.101751    1 0.003419155
14   7 2013  9065.16806  541 0.05967898 16.756318    1 0.003561581
15   8 2012   811.76528  121 0.14905787  6.708804    1 0.022218247
16   8 2013   741.93819  115 0.15499943  6.451636    1 0.024024824
17   9 2012   628.20278  261 0.41547094  2.406907    1 0.172616104
18   9 2013   609.85069  261 0.42797360  2.336593    1 0.183161406
19  10 2012    27.29236    6 0.21984173  4.548727    1 0.048330388
20  10 2013    39.00694    5 0.12818230  7.801389    1 0.016430703
21  11 2012 15305.52292 2324 0.15184061  6.585853    1 0.023055572
22  11 2013 14206.54722 2317 0.16309382  6.131440    1 0.026599593
23  12 2012  1545.67986  202 0.13068683  7.651881    1 0.017079047
24  12 2013  1408.35000  214 0.15195086  6.581075    1 0.023089065
25  13 2012  1067.82847  333 0.31184784  3.206692    1 0.097249074
26  13 2013  1151.11875  327 0.28407147  3.520241    1 0.080696602

Upvotes: 2

Views: 57

Answers (1)

Stephan Kolassa
Stephan Kolassa

Reputation: 8267

It looks like your val3 and val4 just happen to multiply to 1 within machine accuracy:

summary(testset$val3 * testset$val4)

I suggest you rethink where the data come from.

Upvotes: 4

Related Questions