-1

I wrote a for loop to calculate the totals of one variable ("value") for each category of another ("currency"). In essence, I want to know the total value for each currency in my data set ("q4totalnetassets").

I am able to produce the output that I want, but the output contains an extra component with value 0.000 that shouldn't be in the vector:

Here's my code for the "for loop":

sums = c()
for(i in levels(q4totalnetassets$Currency)){ 
  sums[i] = sum(q4totalnetassets$Value[q4totalnetassets$Currency == i & q4totalnetassets$Country != "TOTAL"])
}
sums

Here's my data:

> q4totalnetassets[, c(1, 8, 9)]
             Country        Value Currency
216            TOTAL 8199900.6820      EUR
1956         Austria   79205.5520      EUR
3696         Belgium   76531.2991      EUR
5436        Bulgaria     795.5987      BGN
7176         Croatia   13857.6950      HRK
8916  Czech Republic  202580.4473      CZK
10656        Denmark  804999.1900      DKK
12396         France  762929.0000      EUR
14136        Germany  309851.7970      EUR
15876         Greece    4422.0000      EUR
17616        Hungary  148981.6908      HUF
19356        Ireland 1446873.0000      EUR
21096          Italy  226043.3900      EUR
22836  Liechtenstein   27902.8000      CHF
24576     Luxembourg 2946860.0000      EUR
26316          Malta    2737.3674      EUR
28056    Netherlands   34186.0000      EUR
29796         Poland   92855.8900      PLN
31536       Portugal    7577.3618      EUR
33276        Romania   20974.2320      RON
35016       Slovakia    3973.8010      EUR
36756       Slovenia    2308.6453      EUR
38496          Spain  185420.0000      EUR
40236    Switzerland  444451.1453      CHF
41976         Turkey   37598.8612      TRY
43716 United Kingdom  795221.1740      GBP
45456        Finland   78619.0081      EUR
47196         Sweden 2411741.0000      SEK
48936         Norway  904348.0000      NOK

Any ideas on why the extra category is appearing?

Vincent Bonhomme
  • 7,235
  • 2
  • 27
  • 38
  • 1
    1- Please don't add pictures of your results- instead just show them as code (similarly to how you did so far). 2- See [here](http://stackoverflow.com/questions/1195826/drop-factor-levels-in-a-subsetted-data-frame) if this solves your problem – David Arenburg Mar 27 '16 at 11:45
  • Please make your [example reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Roman Luštrik Mar 27 '16 at 13:45

1 Answers1

0

Instead of the for loop, we could use one of the group by methods. With data.table, convert 'data.frame' to 'data.table' (setDT(q4totalnetassets)), grouped by 'Currency', we get the sum of 'Value' where the "Country" value is not "TOTAL".

library(data.table)
setDT(q4totalnetassets)[Country!="TOTAL",
              .(Sums = sum(Value)), by = Currency]
#    Currency         Sums
# 1:      EUR 6167538.2217
# 2:      BGN     795.5987
# 3:      HRK   13857.6950
# 4:      CZK  202580.4473
# 5:      DKK  804999.1900
# 6:      HUF  148981.6908
# 7:      CHF  472353.9453
# 8:      PLN   92855.8900
# 9:      RON   20974.2320
#10:      TRY   37598.8612
#11:      GBP  795221.1740
#12:      SEK 2411741.0000
#13:      NOK  904348.0000

NOTE: The above method will not provide any 0 values for unused levels in the 'Currency'. Regarding the for loop, it could be a issue with unused levels as mentioned in the comments.

akrun
  • 874,273
  • 37
  • 540
  • 662