title: Summing over columns with if statement in R
body: <p>I have a data that looks like this.</p>
  
  <pre><code>bankname    bankid  year    totass  corresbankname1 corresbankloc1  corresdepoin1   corresbankname2 corresbankloc2  corresdepoin2   corresbankname3 corresbankloc3  corresdepoin3   
  BankA   1   1881    244789  First Bank  New York    7250.32 Third National Bank Philadelphia    20218.2 Commercial Bank Philadelphia    29513.4   
  
  BankB   2   1881    195755  National Bank Pittsburgh    10243.6 Union Trust Company New York    1851.51 NA  NA  NA   
  
  Bankc   3   1881    107736  Mechanics' Bank New York    13357.8 Wyoming Bank    Wilkes-Barre    17761.2 NA  NA  NA      
  
  BankD   4   1881    170600  Commonwealth Bank   Philadelphia    3.35    Seventh National Bank   Philadelphia    2   NA  NA  NA  
  
  BankE   5   1881    320000  National Bank   New York    351266  Mechanics'  Bank    New York    314012  National Park Bank  New York    206580
  </code></pre>
  
  <p>This can be replicated by </p>
  
  <pre><code>bankname &lt;- c("The Anchor Savings Bank of Pittsburgh","The Arsenal Bank","The Ashley Savings Bank","The Bank of America of Philadelphia","The Bank of Pittsburgh")
  bankid &lt;- c( 1, 2,  3,  4,  5)
  year&lt;- c( 1881, 1881,   1881,   1881,   1881)
  totass  &lt;- c(244789,    195755, 107736, 170600, 32000000)
  corresbankname1 &lt;- c("First National Bank","National Bank of Commerce","Mechanics' National Bank","Commonwealth National Bank","National Bank of Commerce")
  corresbankloc1 &lt;-c("Philadelphia","Pittsburgh","New York","Philadelphia","New York")
  corresdepoin1&lt;-c(7250.32,10243.6,13357.8,3.35,351266)
  corresbankname2 &lt;- c("Third National Bank","Union Trust Company","Wyoming National Bank","Seventh National Bank","Mechanics' National Bank")
  corresbankloc2&lt;-c("New York","New York","Wilkes-Barre","Philadelphia","New York")
  corresdepoin2&lt;-c(20218.2,1851.51,17761.2,2,314012)
  corresbankname3&lt;-c("Commercial National Bank",NA,NA,NA,"National Park Bank")
  corresbankloc3&lt;-c("Philadelphia",NA,NA,NA,"New York")
  corresdepoin3&lt;-c(29513.4,NA,NA,NA,206580)
  bankdata&lt;-data.frame(bankname, bankid,year,totass,corresbankname1,corresbankloc1,corresdepoin1,corresbankname2,corresbankloc2,corresdepoin2,corresbankname3,corresbankloc3,corresdepoin3)
  </code></pre>
  
  <p>This dataset shows the amount(corresdepoin) each bank invested in other banks (corresbankname) and its location (corresbankloc). I have 43 corresbankname, corresbankloc and corresdepoin variables. </p>
  
  <p>Since these banks invest in multiple banks in the same city, I would like to know the total amount of investments in each city. Hence, I would like to generate a new column variable called "total_New York" and sum amount indicated by corresdepoin if correspobankloc is New York. How can I loop over 43 variables?</p>
  
  <p>For instance, BankE has $351266 (corresdepoin1) at National Bank(corresbankname1) in New York(corresbankloc1), $314012 at Mechenics' Bank in New York, and $206580 at National Park Bank in New York. I want a new column called "total deposits in New York" showing that the total amount of investments in banks located in New York is $871858. Hence, what I want is a conditional statement that loops over columns (corresponbankloc) and checks if this is New York or others, and then sum associated values in "corresdepoin" to get aggregated amount of total investment in that city for each "bankname."</p>
  
  <p>Also, in stata, if I want to do this for multiple cities, I would generate<br>
  local cities "New York" "Philadelphia" "Pittsburgh"<br>
  and loop over them. Is there a similar function in R?   </p>
  
  <p>Thank you in advance.</p>