Loop through multiple levels of a variable in R

51 views Asked by At

I need to be able to perform basic math operations across my census tract data in R. So for example, for CT13121009501 I need to multiply grade*PPT that correspond to that census tract, then I need to divide that result by the PPT(total sum for that census tract): [1(0.379) + 1(0.296)...+ 5(0.00946) ] / 0.379 +0.296 + ... + 0.00946. I need to do that for every census tract and get the result in a new column (one per census tract). I have tried different loops but nothing works. Anyone have any ideas? I have started to just do it manually in excel but I am trying to learn R.

           CT Grade       PPT
         <dbl> <dbl>     <dbl>
 13121009501     1   0.379  
          NA     1   0.296  
          NA     2   0.0807 
          NA     3   0.00797
          NA     5   0.00946    
 13121003700     4 100      
 13121011202     3   0.414  
          NA     4   0.0562 
          NA     5   0.105  
 13121011100     2   0.0847 
          NA     3   0.0650 
          NA     3   0.0469 
          NA     6   0.145  
 13121011201     3   0.175  
          NA     3   0.146  

I was able to do it separately for a single census tract but not to loop it through all of them.

0

There are 0 answers